java spring整合junit操作(有详细的分析过程)

 更新时间:2020年8月18日 08:07  点击:2213

此博客解决了什么问题:

解决测试的时候代码冗余的问题,解决了测试工程师的编码能力可能没有开发工程师编码能力的问题,解决了junit单元测试和spring注解相结合!

测试类代码:(只给大家展示测试类的代码)

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 testFindSave(){
  Account account=new Account();
  account.setMoney(20000f);
  account.setName("test");
  //1.获取容器
  ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
  //2.得到业务层对象
  IAccountService as =ac.getBean("accountService",IAccountService.class);
  as.saveAccount(account);
 }
 @Test
 public void testFindUpdate(){
  Account account=new Account();

  //1.获取容器
  ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
  //2.得到业务层对象
  IAccountService as =ac.getBean("accountService",IAccountService.class);
  account=as.findAccountById(4);
   account.setMoney(40000f);
  as.updateAccount(account);
 }
}

以上的代码都有公共的地方:

 //1.获取容器
 ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
 //2.得到业务层对象
 IAccountService as =ac.getBean("accountService",IAccountService.class);

此时为了减少代码的冗余我们完全可以将其抽离出来,如下:

 private ApplicationContext ac;
 private IAccountService as;

 @Before
 public void init(){
  //1.获取容器
   ac=new ClassPathXmlApplicationContext("bean.xml");
  //2.得到业务层对象
   as =ac.getBean("accountService",IAccountService.class);
 }

 @Test
 public void testFindAll(){

  //3.执行方法
   List<Account> accounts=as.findAllAccount();
   for(Account account:accounts){
    System.out.println(account);
   }
 }
 @Test
 public void testFindSave(){
  Account account=new Account();
  account.setMoney(20000f);
  account.setName("test");
  as.saveAccount(account);
 }
 @Test
 public void testFindUpdate(){
  Account account=new Account();
  account=as.findAccountById(4);
   account.setMoney(40000f);
  as.updateAccount(account);
 }

上面的代码似乎解决了我们的问题,但是我们忽略了一个问题,就是说在软件开发的过程中,这是两个角色,开发代码的是软件开发工程师,而这个测试的为软件测试工程师,对于测试人员只管方法能不能执行,性能怎么样,上面抽离出的代码测试人员不一定会写!

 private ApplicationContext ac;
 private IAccountService as;

 @Before
 public void init(){
  //1.获取容器
   ac=new ClassPathXmlApplicationContext("bean.xml");
  //2.得到业务层对象
   as =ac.getBean("accountService",IAccountService.class);
 }

分析:

首先我们先明确三点:

1.一般应用程序的入口都有main方法,但是在junit单元测试中,没有main方法也能执行,junit集成了一个main方法,该方法就会判断当前测试类中 是否有@test注解,然后让带着Test注解的类执行。

2、junit不会管我们是否采用spring框架,在执行测试方法时,junit根本不知道我们是不是使用了spring框架,所以也就不会为我们读取配置文件/配置类创建spring核心容器

3.当测试方法执行时,没有Ioc容器,就算写了Autowired注解,也无法实现注入

综上所述:按照我们之前的Autowried注入已经不好使了!接下看解决办法:

1.导入spring整合junit的jar(坐标)

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>5.0.2.RELEASE</version>
  </dependency>

2.使用junit提供的一个注解把原有的main方法替换了,替换成spring提供的,

这个注解是@RunWith,然后网上有这样的解释,我觉得比较贴切:

@RunWith就是一个运行器

@RunWith(JUnit4.class)就是指用JUnit4来运行

@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境,以便在测试开始的时候自动创建Spring的应用上下文

注解了@RunWith就可以直接使用spring容器,直接使用@Test注解,不用启动spring容器

@RunWith(Suite.class)的话就是一套测试集合

3.告知spring的运行器,spring创建是基于xml还是注解的,并说明位置

这个注解就是:@ContextConfiguration

locations:指定xml文件的位置,加上classpath关键字,表示在类路径下

classes: 指定注解类所在地位置

当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)

public class AccountServiceTest {
 @Autowired
 private IAccountService accountService;
 
 @Test
 public void testFindAll() {
  //3.执行方法
  List<Account> accounts = accountService.findAllAccount();
  for(Account account : accounts){
   System.out.println(account);
  }
 }

 @Test
 public void testSave() {
  Account account = new Account();
  account.setName("test anno");
  account.setMoney(12345f);
  //3.执行方法
  accountService.saveAccount(account);
 }

 @Test
 public void testUpdate() {
  //3.执行方法
  Account account = accountService.findAccountById(4);
  account.setMoney(23456f);
  accountService.updateAccount(account);
 }
}

补充知识:idea Could not autowire. No beans of 'XXXX' type found.

如下图:在使用@Autowired注解的时候,提示找不到bean类型,查找了半天错误,发现这就不是错误,因为它根本不会影响程序的运行! 此时我以为是我的Service层注解没写,可是明明写了!看下面的解决办法!

解决办法:

点击文件–setting–Editor–Inspections–spring–Warning–Apply–OK

以上这篇java spring整合junit操作(有详细的分析过程)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章