Spring 结合 junit4 编写单元测试


#Spring


示例

项目结构

├── README.md
├── build.gradle
├── settings.gradle
└── src
    ├── main   // 项目代码
    │   ├── java
    │   │   └── demo
    │   │       ├── AppConfig.java
    │   │       ├── Calculate.java
    │   │       └── Main.java
    │   └── resources
    │       └── beans.xml
    └── test   // 测试代码
        └── java
            └── demo
                ├── BaseTest.java
                ├── CalculateTestV1.java
                ├── CalculateTestV2.java
                └── CalculateTestV3.java

build.gradle

build.gradle 中引入的依赖如下:

dependencies {
    compile group: 'org.springframework', name: 'spring-core', version: '5.0.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-context', version: '5.0.6.RELEASE'

    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile group: 'org.springframework', name: 'spring-test', version: '5.0.6.RELEASE'
}

beans.xml

beans.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="demo" />

</beans>

AppConfig.java

AppConfig 内容如下:

package demo;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "demo")
public class AppConfig {
}

Calculate.java

Calculate 类内容如下:

package demo;

import org.springframework.stereotype.Component;

@Component
public class Calculate {

    public int add(int a, int b) {
        return a+b;
    }
}

Main.java

Main 类内容如下:

package demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    // 用 xml 配置文件加载 ApplicationContext,获取bean
    public static void useXml() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        Calculate calculate = (Calculate) ctx.getBean(Calculate.class);
        System.out.println(calculate.add(1,2));
    }

    // 通过配置类加载 ApplicationContext,获取bean
    public static void useAnnotation() {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        Calculate calculate = (Calculate) ctx.getBean(Calculate.class);
        System.out.println(calculate.add(1,2));
    }

    public static void main(String[] args) {

        useXml();
        useAnnotation();

    }

}

运行输出 :

3
3

测试方式1

要在将Spring和junit整合起来,很简单,给测试类加上@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration 即可,@ContextConfiguration 的属性中要指定xml配置文件,或者配置类。

测试代码中的 CalculateTestV1 类内容如下:

package demo;

import org.junit.Assert;
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.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:beans.xml")
public class CalculateTestV1 {

    @Autowired
    private Calculate calculate;

    @Test
    public void test() {
        Assert.assertEquals(3, calculate.add(1, 2));
    }

}

该测试类通过 xml 配置文件加载 Spring 加载应用上下文。

@RunWith(SpringJUnit4ClassRunner.class) 换成 @RunWith(SpringRunner.class) 也是可以的。SpringRunner 类单纯的继承了 SpringJUnit4ClassRunner 类。

测试方式2

测试代码中的 CalculateTestV2 类内容如下:

package demo;

import org.junit.Assert;
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.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=AppConfig.class)
public class CalculateTestV2 {

    @Autowired
    private Calculate calculate;

    @Test
    public void test() {
        Assert.assertEquals(3, calculate.add(1, 2));
    }

}

该测试类通过配置类加载 Spring 加载应用上下文。

测试方式3

整合了spring的测试类是可以继承的。

测试代码中的 CalculateTestV3 类继承自 BaseTest

BaseTest 内容如下:

package demo;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=AppConfig.class)
public class BaseTest {

    @Test
    public void test() {

    }

}

CalculateTestV3 内容如下:

package demo;

import org.junit.Assert;
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.SpringJUnit4ClassRunner;

public class CalculateTestV3 extends BaseTest{

    @Autowired
    private Calculate calculate;

    @Test
    public void test() {
        Assert.assertEquals(3, calculate.add(1, 2));
    }

}


( 本文完 )