当前位置: 移动技术网 > IT编程>开发语言>Java > java spring整合junit操作(有详细的分析过程)

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

2020年08月18日  | 移动技术网IT编程  | 我要评论
此博客解决了什么问题:解决测试的时候代码冗余的问题,解决了测试工程师的编码能力可能没有开发工程师编码能力的问题,解决了junit单元测试和spring注解相结合!测试类代码:(只给大家展示测试类的代码

此博客解决了什么问题:

解决测试的时候代码冗余的问题,解决了测试工程师的编码能力可能没有开发工程师编码能力的问题,解决了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操作(有详细的分析过程)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网