- I have a maven project the uses both TestContainers (latest 1.5.2) and JUnit 5 (5.7.0) tests. Surefire plugin set to version 2.22.2 (see full POM below). Maven version is 3.6.0 and using Java 11.
- I created two test classes: one uses JUnit 4.12 (available through TestContainers dependencies) and the other uses JUnit 5.7.0
- When the TestContainers dependencies in POM are defined, a call to
mvn test
only detects the JUnit 4.12 tests - When the TestContainers dependencies in POM are commented out, a call to
mvn test
detects both JUnit 4.12 and JUnit 5.7.0 tests - adding an
exclusion
to the TestContainers dependency to exclude JUnit 4.12 allows maven to detect and run JUnit 5 tests, BUT it throws an exception from any call to 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>
Example JUnit5 test with a call to TestContainers API:
When excluding Junit 4
from TestContainer dependency, JUnit5 tests are detected, but adding a call to any TestContainers API results in a compilation error:
@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");
}
}
Compilation error details:
[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