JUnit4 & TestNG 与 Spring 整合

最近因为项目须要,开始学习单元测试。可是卡在了测试框架与 Spring 整合的地方,无法启动 Spring 容器,致使测试类中使用的 bean 没法自动注入。查了不少资料,最后把 JUnit4TestNG 与 Spring 整合的问题都解决了。php

前置准备

引入 jar 包

咱们须要扫描类,须要 spring-contextjava

Spring 须要与测试框架整合,因此须要 spring-testspring

使用 JUnit4 测试时,须要引入 junitapache

使用 TestNG 时,须要引入 testngapp

完整的 pom.xml 文件内容以下:框架

<?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>com.zcl</groupId>
  <artifactId>test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.6.RELEASE</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.14.3</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

</project>
复制代码

建立待测试方法

新建一个类 Greetingmaven

package com.zcl;

import org.springframework.stereotype.Component;

/** * @author: changle * @time: 2019-06-18 05:16 */
@Component
public class Greeting {

    public void sayHello() {
        System.out.println("你好呀");
    }

}
复制代码

类中只包含一个简单的方法,咱们用这个方法进行测试。单元测试

建立 Spring 配置文件

resources 文件夹下建立 applicationContext.xml 文件学习

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="com.zcl"/>

</beans>
复制代码

只添加包扫描就好了。测试

JUnit4

在 IDEA ,咱们在类名上按下 command + shift + t,能够快捷的在 test 文件夹下建立包名相同的测试类。

建立测试类
这里还能够选择生成一些可能会用到的辅助方法。在 Testing library 中能够选择用到的测试框架,很是的方便。

咱们在生成的类的基础上编写一个简单的测试方法并添加可以整合 Spring 的一些配置。

package com.zcl;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/** * @author: changle * @time: 2019-06-18 05:19 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class GreetingTest extends AbstractJUnit4SpringContextTests {

    @Autowired
    private Greeting greeting;

    @Test
    public void testSayHello() {
        System.out.println("JUnit4 测试开始");
        greeting.sayHello();
        System.out.println("JUnit4 测试结束");
    }
}
复制代码

首先是 @RunWith 注解,JUnit 用例都是在 Runner(运行器)中执行的。经过这个注解,咱们能够为测试类指定咱们须要的 Runner,由于是和 Spring 整合,因此选择 SpringJUnit4ClassRunner

接下来是 @ContextConfiguration。 这个注解会加载咱们填入的文件,以便注入须要使用的 bean。

咱们的类继承了 Spring 提供的 AbstractJUnit4SpringContextTests 这个接口,用于获取 Application Context

运行测试用例,最后获得的结果如图所示:

JUnit4 结果

TestNG

依然是建立测试用例,与 JUnit 的区别只是获取Application Context的方式不一样。

package com.zcl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

/** * @author: changle * @time: 2019-06-18 05:45 */
@ContextConfiguration("classpath:applicationContext.xml")
public class GreetingTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private Greeting greeting;

    @Test
    public void testSayHello() {
        System.out.println("TestNG 测试开始");
        greeting.sayHello();
        System.out.println("TestNG 测试结束");
    }
}
复制代码

一样有采用 @ContextConfiguration 来引入 Spring 配置文件。

TestNG 再也不须要加入 Runner

测试类继承的是 AbstractTestNGSpringContextTests,这样才能够访问到Application Context

运行测试用例,获得的结果以下图

TestNG 测试用例

总结

其实这两种框架与 Spring 集成须要的配置差很少,学会了测试框架与 Spring 集成以后,咱们就能够方便地写单元测试了。 这两个测试框架都还支持不少种类的测试,以后会继续分享它们的其余功能的使用案例。

相关文章
相关标签/搜索