首先我们来看一下上一次我们进行账户CRUD操作时的测试代码:
/**
* @Author: Ly
* @Date: 2020-07-27 17:17
*/
public class AccountServiceTest {
@Test
public void testFindAll(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
List<Account> accounts=as.findAllAccount();
for(Account account:accounts){
System.out.println(account);
}
}
@Test
public void testFindOne(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
Account account=as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave(){
Account account=new Account();
account.setName("sss");
account.setMoney(123f);
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
as.saveAccount(account);
}
@Test
public void testUpdate(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
Account account=as.findAccountById(4);
account.setMoney(2131f);
as.updateAccount(account);
}
@Test
public void testDelete(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
as.deleteAccount(4);
}
}
在上边代码中中我们可以看出,每一个测试模块中都有着重复的代码:
采用Spring整合Junit:
相关配置:
1. 导入spring整合junit的jar包(坐标)
2. 使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的@Runwith
3. 告知spring的运行器,spring和ioc创建是基于xml或注解的,并且说明位置
- @ContextConfiguration:
Location:指定xml文件的位置,加上classpath关键字,表示在类路径下
classes:指定注解所在的位置
- @ContextConfiguration:
注意:当我们使用spring 5.x版本的时候,要求junit版本必须是4.12以上
1、在pom.xml中导入spring整合junit的jar包
<?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>org.example</groupId>
<artifactId>spring_day02_account_anno_withoutxml</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
......
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
......
<dependencies>
</project>
2、使用Junit提供的注解把原有的main方法替换
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean.xml")
//@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest2 {
@Autowired
private IAccountService as;
@Test
public void testFindAll(){
// //1.获取容器
// ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
// //2.得到业务层对象
// IAccountService as=ac.getBean("accountService",IAccountService.class);
//3.执行方法
List<Account> accounts=as.findAllAccount();
for(Account account:accounts){
System.out.println(account);
}
}
@Test
public void testFindOne(){
Account account=as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave(){
Account account=new Account();
account.setName("sss");
account.setMoney(123f);
as.saveAccount(account);
}
@Test
public void testUpdate(){
Account account=as.findAccountById(4);
account.setMoney(2131f);
as.updateAccount(account);
}
@Test
public void testDelete(){
as.deleteAccount(4);
}
}