Spring--整合Junit

首先我们来看一下上一次我们进行账户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:指定注解所在的位置

注意:当我们使用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);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙源lll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值