运营相关

Spring Boot 基于 JUnit 5 实现多模块工程单元

2020-04-24  本文已影响0人  又语

Spring Boot 基于 JUnit 5 实现单元测试 文中已介绍在单个 Spring Boot 应用中如何使用 JUnit 5 完成单元测试,本文介绍当 Spring Boot 应用作为多模块工程的一个模块组件时,如何使用 JUnit 5 实现单元测试。


目录


工程模块说明

创建一个如下结构的工程:

spring-boot-junit5-multi-modules:工程根目录
|-- application:Spring Boot 应用,独立运行服务

示例

  1. 创建 Maven 工程 spring-boot-junit5-multi-modules

  2. spring-boot-junit5-multi-modules 工程下新建模块(Module),选中 Spring Initializr 创建 Spring Boot 应用 application,修改自动生成的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-boot-junit5-multi-modules</artifactId>
        <groupId>tutorial.spring.boot</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>application</artifactId>

    <properties>
        <spring.boot.version>2.2.6.RELEASE</spring.boot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <mainClass>
                        tutorial.spring.boot.Application
                    </mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
  1. 修改主工程 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>tutorial.spring.boot</groupId>
    <artifactId>spring-boot-junit5-multi-modules</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>application</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  1. application 中创建一个 Service 层接口。
package tutorial.spring.boot.service;

public interface DemoService {
}
  1. 创建 Service 层接口实现。
package tutorial.spring.boot.service.impl;

import org.springframework.stereotype.Service;
import tutorial.spring.boot.service.DemoService;

@Service
public class DemoServiceImpl implements DemoService {
}
  1. 创建 Service 层单元测试,测试 Service 层实例是否已生成。
package tutorial.spring.boot.service;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class DemoServiceTest {

    @Autowired
    private DemoService demoService;

    @Test
    public void testNotNull() {
        Assertions.assertThat(demoService).isNotNull();
    }
}
  1. 在 IDE 中直接运行单元测试(Run 'DemoServiceTest'),测试通过(过程略)。

  2. 在项目根目录下运行 mvn test,测试失败,日志如下:

[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ application ---
[INFO] Surefire report directory: D:\spring-boot-junit5-multi-modules\application\target\surefire-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running tutorial.spring.boot.service.DemoServiceTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.246 sec <<< FAILURE!
tutorial.spring.boot.service.DemoServiceTest.testNotNull()  Time elapsed: 0.246 sec  <<< FAILURE!
java.lang.AssertionError: 
Expecting actual not to be null
    at tutorial.spring.boot.service.DemoServiceTest.testNotNull(DemoServiceTest.java:16)
Results :
(..)
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

从日志中可以看出,单元测试并未运行 Spring 上下文,导致找不到依赖 Spring IOC 注入的实例对象。

分析:
Maven 运行 JUnit 5 单元测试需要添加以下依赖:

前两个是 JUnit 5 相关依赖,已通过 spring-boot-starter-test 引入。maven-surefire-plugin 必须使用 2.22.0 及以上版本。

  1. 修改主工程 pom.xml 文件,添加 maven-surefire-plugin 依赖。
<?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>tutorial.spring.boot</groupId>
    <artifactId>spring-boot-junit5-multi-modules</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>application</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

</project>
  1. 在项目根目录下再次运行 mvn test,测试成功,从日志中可以看出已启动 Spring 上下文(测试日志略)。
上一篇下一篇

猜你喜欢

热点阅读