日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring整合junit问题分析

發(fā)布時(shí)間:2024/4/13 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring整合junit问题分析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

spring整合Junit分析

1、應(yīng)用程序的入口
?? ?main方法
2、junit單元測試中,沒有main方法也能執(zhí)行
?? ?junit集成了一個(gè)main方法
?? ?該方法就會判斷當(dāng)前測試類中哪些方法有 @Test注解
?? ?junit就讓有Test注解的方法執(zhí)行
3、junit不會管我們是否采用spring框架
?? ?在執(zhí)行測試方法時(shí),junit根本不知道我們是不是使用了spring框架
?? ?所以也就不會為我們讀取配置文件/配置類創(chuàng)建spring核心容器
4、由以上三點(diǎn)可知
?? ?當(dāng)測試方法執(zhí)行時(shí),沒有Ioc容器,就算寫了Autowired注解,也無法實(shí)現(xiàn)注入

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>day02_learn_04account_annoioc_withoutxml</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.45</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies> </project> package com.learn.domain;import java.io.Serializable;/*** 賬戶的實(shí)體類*/ public class Account implements Serializable {private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';} } package com.learn.service;import com.learn.domain.Account;import java.util.List;/*** 賬戶的業(yè)務(wù)層接口*/ public interface IAccountService {/*** 查詢所有* @return*/List<Account> findAllAccount();/*** 查詢一個(gè)* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 刪除* @param acccountId*/void deleteAccount(Integer acccountId);} package com.learn.service.impl;import com.learn.dao.IAccountDao; import com.learn.domain.Account; import com.learn.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類*/ @Service("accountService") public class AccountServiceImpl implements IAccountService{@Autowiredprivate IAccountDao accountDao;public List<Account> findAllAccount() {return accountDao.findAllAccount();}public Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}public void saveAccount(Account account) {accountDao.saveAccount(account);}public void updateAccount(Account account) {accountDao.updateAccount(account);}public void deleteAccount(Integer acccountId) {accountDao.deleteAccount(acccountId);} } jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=123456 package config;import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.commons.dbutils.QueryRunner; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Scope;import javax.sql.DataSource;/*** 和spring連接數(shù)據(jù)庫相關(guān)的配置類*/ public class JdbcConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;/*** 用于創(chuàng)建一個(gè)QueryRunner對象* @param dataSource* @return*/@Bean(name="runner")@Scope("prototype")public QueryRunner createQueryRunner(@Qualifier("ds2") DataSource dataSource){return new QueryRunner(dataSource);}/*** 創(chuàng)建數(shù)據(jù)源對象* @return*/@Bean(name="ds2")public DataSource createDataSource(){try {ComboPooledDataSource ds = new ComboPooledDataSource();ds.setDriverClass(driver);ds.setJdbcUrl(url);ds.setUser(username);ds.setPassword(password);return ds;}catch (Exception e){throw new RuntimeException(e);}}@Bean(name="ds1")public DataSource createDataSource1(){try {ComboPooledDataSource ds = new ComboPooledDataSource();ds.setDriverClass(driver);ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");ds.setUser(username);ds.setPassword(password);return ds;}catch (Exception e){throw new RuntimeException(e);}} } package com.learn.test;import com.learn.domain.Account; import com.learn.service.IAccountService; import config.SpringConfiguration; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;/*** 使用Junit單元測試:測試我們的配置* Spring整合junit的配置* 1、導(dǎo)入spring整合junit的jar(坐標(biāo))* 2、使用Junit提供的一個(gè)注解把原有的main方法替換了,替換成spring提供的* @Runwith* 3、告知spring的運(yùn)行器,spring和ioc創(chuàng)建是基于xml還是注解的,并且說明位置* @ContextConfiguration* locations:指定xml文件的位置,加上classpath關(guān)鍵字,表示在類路徑下* classes:指定注解類所在地位置** 當(dāng)我們使用spring 5.x版本的時(shí)候,要求junit的jar必須是4.12及以上*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfiguration.class) public class AccountServiceTest {@Autowiredprivate IAccountService as = null;@Testpublic void testFindAll() {//3.執(zhí)行方法List<Account> accounts = as.findAllAccount();for(Account account : accounts){System.out.println(account);}}@Testpublic void testFindOne() {//3.執(zhí)行方法Account account = as.findAccountById(1);System.out.println(account);}@Testpublic void testSave() {Account account = new Account();account.setName("test anno");account.setMoney(12345f);//3.執(zhí)行方法as.saveAccount(account);}@Testpublic void testUpdate() {//3.執(zhí)行方法Account account = as.findAccountById(4);account.setMoney(23456f);as.updateAccount(account);}@Testpublic void testDelete() {//3.執(zhí)行方法as.deleteAccount(4);} } package com.learn.test;import config.SpringConfiguration; import org.apache.commons.dbutils.QueryRunner; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** 測試queryrunner是否單例*/ public class QueryRunnerTest {@Testpublic void testQueryRunner(){//1.獲取容易ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);//2.獲取queryRunner對象QueryRunner runner = ac.getBean("runner",QueryRunner.class);QueryRunner runner1 = ac.getBean("runner",QueryRunner.class);System.out.println(runner == runner1);} }

?

總結(jié)

以上是生活随笔為你收集整理的spring整合junit问题分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。