在多模块 Maven 项目中共享集成测试

Jen*_*Jen 3 java spring maven kotlin

考虑一个具有三个模块 的 Maven 项目CommonServiceA其中ServiceB两个服务都依赖Common并产生作为单独的微服务部署的战争。对于每个服务,我想运行集成测试,检查服务在/health路径上公开运行状况检查端点。

@Test
open fun testHealthCheck() {
    // implicitly asserts that response status is 200
    perform("/health", method = RequestMethod.GET)
}
Run Code Online (Sandbox Code Playgroud)

我能想到的唯一解决方案是将这个测试复制到每个服务的测试包中。然而,这不是很干。我希望将此集成测试定义在单个位置(最好在Common模块中),但在每个服务的集成测试阶段运行。

我怎样才能做到这一点?

zfo*_*rgo 5

有多种方法可以实现这一目标。两者各有利弊。

解压共享测试

Maven 可以将多个工件附加到每个模块(例如源或测试等)。第一步是附加共享模块的测试类。

<build>
    <plugins>
        <!-- ... -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <!-- ... -->
</build>
Run Code Online (Sandbox Code Playgroud)

之后,它很容易在其他项目中作为依赖项使用:

<dependencies>
    <dependency>
        <groupId>io.github.zforgo.stackoverflow</groupId>
        <artifactId>shared-tests</artifactId>
        <version>0.1.0-SNAPSHOT</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

在依赖项目或模块中共享测试应该解压。

<build>
    <plugins>
        <!-- ... -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <id>share-tests</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>io.github.zforgo.stackoverflow</groupId>
                                <artifactId>shared-tests</artifactId>
                                <version>0.1.0-SNAPSHOT</version>
                                <classifier>tests</classifier>
                                <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <!-- ... -->
</build>
Run Code Online (Sandbox Code Playgroud)

注意ArtifactItem忽略type属性,因此classifier应该使用该属性而不是type. 问题跟踪系统存在问题maven-dependency-pluginMDEP-732

在单元或集成测试阶段,解压的类将运行。

[INFO] ---------------< io.github.zforgo.stackoverflow:project >---------------
[INFO] Building project 0.1.0-SNAPSHOT                                    [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.1.2:unpack (share-tests) @ project ---
[INFO] Configured Artifact: io.github.zforgo.stackoverflow:shared-tests:tests:0.2.0-SNAPSHOT:jar
[INFO] Unpacking /work/source/stackoverflow/junit-test-sharing/multimodule-test-sharing/shared-tests/target/shared-tests-0.1.0-SNAPSHOT-tests.jar to /work/source/stackoverflow/junit-test-sharing/multimodule-test-sharing/project/target/test-classes with includes "" and excludes ""
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ project ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running io.github.zforgo.stackoverflow.shared.SharedUnitTest
>>>> THIS IS A SHARED UNIT TEST <<<<
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 s - in io.github.zforgo.stackoverflow.shared.SharedUnitTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --- maven-failsafe-plugin:3.0.0-M5:integration-test (default) @ project ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running io.github.zforgo.stackoverflow.shared.SharedIntegrationTestIT
>>>> THIS IS A SHARED INTEGRATION TEST <<<<
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in io.github.zforgo.stackoverflow.shared.SharedIntegrationTestIT
Run Code Online (Sandbox Code Playgroud)
优点 缺点
单元测试和集成测试都在一个步骤中共享。 需要解决MDEP-732的原因
共享测试将打包到依赖工件中。防止变化。 类重复使得依赖的测试 jar 不必要地变大。
解包期间支持包含和排除。

使用failsafe和surefire插件的<dependenciesToScan>属性(推荐)

首先,我要感谢@khmarbaise 的建议。

幸运的是两者maven-surefire-plugin都有配置属性。将扫描所有列出的依赖项以查找要包含和运行的测试类。 注意列出的工件必须是依赖项目或模块中的元素。maven-failsafe-plugin<dependenciesToScan><dependency>

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>io.github.zforgo.stackoverflow</groupId>
        <artifactId>shared-tests-aggregator</artifactId>
        <version>0.2.0-SNAPSHOT</version>
    </parent>

    <artifactId>project</artifactId>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>shared-tests</artifactId>
            <version>${project.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <dependenciesToScan>
                        <dependency>${project.groupId}:shared-tests:test-jar</dependency>
                    </dependenciesToScan>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <dependenciesToScan>
                                <dependency>${project.groupId}:shared-tests:test-jar</dependency>
                            </dependenciesToScan>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

注意maven-failsafe-plugin:integration-test如果依赖项目中缺少相同的包,目标将向控制台报告错误的错误消息。

[INFO] 
[INFO] --- maven-failsafe-plugin:3.0.0-M5:integration-test (default) @ project ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[ERROR] WARNING: package io.github.zforgo.stackoverflow.shared not in project
[INFO] Running io.github.zforgo.stackoverflow.shared.SharedIntegrationTestIT
>>>> THIS IS A SHARED INTEGRATION TEST <<<<
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 s - in io.github.zforgo.stackoverflow.shared.SharedIntegrationTestIT
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
Run Code Online (Sandbox Code Playgroud)
优点 缺点
易于分离单元和集成测试共享 共享单元测试和集成测试时必须复制配置
共享测试不会被复制,没有类重复 日志中出现虚假的 [ERROR] 消息,但构建成功。
负责任的插件将处理依赖的测试类 可能的配置会随着版本的不同而随时更改。始终阅读文档

使用build-helper-maven-plugin

此 MojoHaus 插件允许在一个模块或项目中使用多个源或测试源目录。

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>io.github.zforgo.stackoverflow</groupId>
        <artifactId>shared-tests</artifactId>
        <version>0.1.0-SHAPSHOT</version>
    </parent>
    <artifactId>project</artifactId>

    <dependencies>
        <dependency>
            <groupId>io.github.zforgo.stackoverflow</groupId>
            <artifactId>shared-test</artifactId>
            <version>0.1.0-SHAPSHOT</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.parent.basedir}/shared-test/src/test/java/com/example/demo/shared</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)
优点 缺点
仅适用于本地文件夹。不支持外部工件。
文件夹是硬编码的。维护问题。

简而言之,在项目和模块之间共享测试的可能性有更多,但现在轮到您选择最好的一个了。