【java】【spring】整合junit(有详细的分析过程)

此博客解决了什么问题:

解决测试的时候代码冗余的问题,解决了测试工程师的编码能力可能没有开发工程师编码能力的问题,解决了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);
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值