[英]Sharing integration test in multi module Maven project
考虑一个 Maven 项目,它具有三个模块Common
、 ServiceA
、 ServiceB
,其中两个服务都依赖于Common
并产生部署为单独微服务的战争。 对于我想运行的每个服务进行集成测试,检查服务是否在/health
路径上公开了一个健康检查端点。
@Test
open fun testHealthCheck() {
// implicitly asserts that response status is 200
perform("/health", method = RequestMethod.GET)
}
我能想出的唯一解决方案是将此测试复制到每个服务的测试包中。 但是,这不是很干燥。 我希望在一个地方(最好在Common
模块中)定义此集成测试,但要在每个服务的集成测试阶段运行。
我怎样才能做到这一点?
有几种方法可以实现这一目标。 这两者各有利弊。
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>
在此之后,它很容易在其他项目中作为依赖项使用:
<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>
在依赖项目或模块共享测试应该解包。
<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>
注意: ArtifactItem
忽略type
属性,因此应该使用classifier
属性而不是type
。 maven-dependency-plugin
问题跟踪系统存在问题。 MDEP-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
优点 | 缺点 |
---|---|
单元测试和集成测试都在一个步骤中共享。 | 需要解决MDEP-732的原因 |
共享测试将被打包到依赖工件中。 防止更改。 | Class 重复使依赖的测试罐不必要地变大。 |
解包期间支持包含和排除。 |
<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>
注意:如果依赖项目中缺少相同的 package, 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]
优点 | 缺点 |
---|---|
易于分离的单元和集成测试共享 | 共享单元测试和集成测试时必须复制配置 |
共享测试不会被复制,没有 class 重复 | 日志中有虚假的 [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>
优点 | 缺点 |
---|---|
仅适用于本地文件夹。 不支持外部工件。 | |
文件夹是硬编码的。 维护问题。 |
简而言之,在项目和模块之间共享测试有更多的可能性,但轮到你选择最好的了。
专门为集成测试创建一个新的 Maven 模块怎么样? 然后,此模块可以同时依赖ServiceA
和ServiceB
。 然后,您的测试可以作为这个新模块的集成测试阶段的一部分运行。
您可能希望使用 Maven 故障安全插件进行集成测试。 这允许集成测试从一个单独的目标运行。 这样做的原因是针对诸如 Jenkins 之类的持续集成工具,您希望在其中运行单元测试,而不是集成测试,因为例如,它们依赖于运行的外部 web 服务。 如果该服务未运行,您的 CI 构建将失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.