【Spring】基础入门篇(五) 使用SpringTest整合JUnit编写测试类

Spring·基础入门篇(五) 使用SpringTest整合JUnit编写测试类


1. 分析测试中的问题

在测试类中,每个测试方法都有以下两行代码:

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);

这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常。所以又不能轻易删掉。

我们可以把容器的获取定义到类中去:

/**
* 测试类
* @author 黑马程序员
* @Company http://www.ithiema.com
* @Version 1.0
*/
public class AccountServiceTest {
	private ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
	private IAccountService as = ac.getBean("accountService",IAccountService.class);
}

这种方式虽然能解决问题,但是扔需要我们自己写代码来获取Spring容器。

2. 解决思路分析

针对上述问题,我们需要的是程序能自动帮我们创建容器。一旦程序能自动为我们创建 spring 容器,我们就无须手动创建了,问题也就解决了。

我们都知道,junit 单元测试的原理(在 web 阶段课程中讲过),但显然,junit 是无法实现的,因为它自己都无法知晓我们是否使用了 spring 框架,更不用说帮我们创建 spring 容器了。不过好在,junit 给我们暴露了一个注解,可以让我们替换掉它的运行器。

这时,我们需要依靠 spring 框架,因为它提供了一个运行器,可以读取配置文件(或注解)来创建容器。我们只需要告诉它配置文件在哪就行了。

2.1 第一步:导入整合junit 的必备 jar 包

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

此处需要注意的是,导入 jar 包时,需要导入一个 spring 中 aop 的 jar 包

2.2 第二步:使用@RunWith 注解替换原有运行

@RunWith(SpringJUnit4ClassRunner.class)
public class AccountServiceTest {
}

2.3 第三步:使用@ContextConfiguration 指定 spring 配置文件的位置

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:applicationContext.xml"})
public class AccountServiceTest {
}

@ContextConfiguration 注解:

  • locations 属性:用于指定配置文件的位置。如果是类路径下,需要用 classpath:表明
  • classes 属性:用于指定注解的类。当不使用 xml 配置时,需要用此属性指定注解类的位置。

2.3 第四步:使用@Autowired 给测试类中的变量注入数据

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class AccountServiceTest {
	@Autowired
	private IAccountService as ;
}

@Autowired:自动注入Bean,注解那篇会讲

2.4 测试

/**
 * @author: LzCc
 * @blog: https://blog.csdn.net/qq_41744145
 * @description: Spring整合JUnit
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:applicationContext.xml"})
public class AccountServiceTest {
    @Autowired
    IAccountService as;
    /**
     * 测试保存
     */
    @Test
    public void testSaveAccount() {
        Account account = new Account();
        account.setName("勒布朗詹姆斯");
        account.setMoney(100000f);
        as.saveAccount(account);
    }
    /**
     * 测试查询一个
     */
    @Test
    public void testFindAccountById() {
        Account account = as.findAccountById(1);
        System.out.println(account);
    }
    /**
     * 测试更新
     */
    @Test
    public void testUpdateAccount() {
        Account account = as.findAccountById(1);
        account.setMoney(20301050f);
        as.updateAccount(account);
    }
    /**
     * 测试删除
     */
    @Test
    public void testDeleteAccount() {
        as.deleteAccount(1);
    }
    /**
     * 测试查询所有
     */
    @Test
    public void testFindAllAccount() {
        List<Account> list = as.findAllAccount();
        for (Account account : list) {
            System.out.println(account);
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值