- 我有一个使用 TestContainers(最新 1.5.2)和 JUnit 5(5.7.0)测试的 maven 项目。Surefire 插件设置为 2.22.2 版(请参阅下面的完整 POM)。Maven 版本为 3.6.0 并使用 Java 11。
- 我创建了两个测试类:一个使用 JUnit 4.12(可通过 TestContainers 依赖项获得),另一个使用 JUnit 5.7.0
- 当定义 POM 中的 TestContainers 依赖项时,调用
mvn test
仅检测 JUnit 4.12 测试 - 当 POM 中的 TestContainers 依赖项被注释掉时,调用来
mvn test
检测 JUnit 4.12 和 JUnit 5.7.0 测试 - 添加
exclusion
到 TestContainers 依赖项以排除 JUnit 4.12 允许 maven 检测和运行 JUnit 5 测试,但它会从对 TestContainers API 的任何调用中引发异常:class file for org.junit.rules.TestRule not found
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>org.example</groupId>
<artifactId>test-mvn-containers-junit-collision</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.15.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.15.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
调用 TestContainers API 的示例 JUnit5 测试:
从 TestContainer 依赖项中排除Junit 4
时,会检测到 JUnit5 测试,但添加对任何 TestContainers API 的调用会导致编译错误:
@Testcontainers
public class TesterJunit570 {
@Container
private static final GenericContainer<?> redis = new GenericContainer<>("redis:alpine")
.withExposedPorts(6379); // this causes the compilation error below
@Test
public void test1(){ // this JUnit 5 test is detected when Junit 4 is excluded from TestContainers POM dependencies
System.out.println("Test JUnit 5");
}
}
编译错误详情:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project test-mvn-containers-junit-collision: Compilation failure
[ERROR] [...]/src/test/java/events/TesterJunit570.java:[13,13] cannot access org.junit.rules.TestRule
[ERROR] class file for org.junit.rules.TestRule not found