Junit特点
-
应用程序的入口:main方法
-
junit单元测试中,没有main方法也能执行
junit集成了一个main方法,该方法就会判断当前测试类中哪些方法有 @Test注解junit就让有Test注解的方法执行
-
junit不会管我们是否采用spring框架
在执行测试方法时,junit根本不知道我们是不是使用了spring框架,所以也就不会为我们读取配置文件/配置类创建spring核心容器 -
由以上三点可知:当测试方法执行时,没有Ioc容器,就算写了Autowired注解,也无法实现注入
Spring整合Junit配置
1. 导坐标
导入spring整合Junit的jar(坐标)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
2. @RunWith
使用Junit提供的一个注解,替换原来的main方法,替换成spring提供的
@RunWith(SpringJUnit4ClassRunner.class)
3.@ContextConfiguration
告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置
locations
:指定xml文件的位置,加上classpath
关键字,表示在类路径下
@ContextConfiguration(locations = "classpath:bean.xml")
classes
:指定注解类所在地位置
@ContextConfiguration(classes = SpringConfiguration.class)
4. 注意junit版本
当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
基于注解的整合案例
代码演示:
1. 基于注解的spring原始Junit的案例
public class AccountServiceTest {
@Test
public void testFindOne() {
// 1.获取容器
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class, JdbcConfig.class);
// 2.得到业务对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
// 3.执行方法
//3.执行方法
Account account = as.findAccountById(1);
System.out.println(account);
}
}
2. 基于注解的spring整合Junit的案例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
/**
* spring整合Junit配合@Autowired实现简化操作 【注释掉前两行无用代码即可】
*/
@Autowired
private IAccountService as = null;
@Test
public void testFindOne() {
//3.执行方法
Account account = as.findAccountById(1);
System.out.println(account);
}
}
基于xml的整合案例
1. 基于xml的spring原始Junit的案例
public class AccountServiceTest {
@Test
public void testFindOne() {
// 1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 2.得到业务对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
// 3.执行方法
//3.执行方法
Account account = as.findAccountById(1);
System.out.println(account);
}
}
2. 基于xml的spring整合Junit的案例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
/**
* spring整合Junit配合@Autowired实现简化操作 【注释掉前两行无用代码即可】
*/
@Autowired
private IAccountService as = null;
@Test
public void testFindOne() {
//3.执行方法
Account account = as.findAccountById(1);
System.out.println(account);
}
}
主配置文件
@Configuration
//@ComponentScan(basePackages = {"cn.luis"})
@ComponentScan("cn.luis")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}
子配置文件
/**
* 和spring连接数据库相关的配置类
**/
@Configuration
public class JdbcConfig {
/**
* spel表达式
*/
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.user}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 用于创建一个QueryRunner对象
* 注入数据源 :构造函数注入 ref:构造函数的参数
* @param dataSource
* @return
*/
@Bean(name="runner")
@Scope("prototype")
//@Qualifier("ds2"):注解到方法参数上来选择数据源 【一个对象有多个实现类的情况】
public QueryRunner createQueryRunner(@Qualifier("ds2") DataSource dataSource){
return new QueryRunner(dataSource);
}
/**
* 创建数据源对象
* @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/eesy02");
ds.setUser(username);
ds.setPassword(password);
return ds;
}catch (Exception e){
throw new RuntimeException(e);
}
}
}