微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当 pom.xml 中存在 TestContainers 依赖项时,未检测到 JUnit 5 测试

如何解决当 pom.xml 中存在 TestContainers 依赖项时,未检测到 JUnit 5 测试

  1. 我有一个 maven 项目,它同时使用 TestContainers(最新 1.5.2)和 JUnit 5 (5.7.0) 测试。 Surefire 插件设置为 2.22.2 版(请参阅下面的完整 POM)。 Maven 版本为 3.6.0,使用 Java 11。
  2. 我创建了两个测试类:一个使用 JUnit 4.12(可通过 TestContainers 依赖项获得),另一个使用 JUnit 5.7.0
  3. 定义 POM 中的 TestContainers 依赖项后,对 mvn test调用仅检测 JUnit 4.12 测试
  4. 当 POM 中的 TestContainers 依赖项被注释掉时,对 mvn test调用会检测到 JUnit 4.12 和 JUnit 5.7.0 测试
  5. 向 TestContainers 依赖项添加 exclusion 以排除 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

解决方法

更新:

通过使用版本 surefire 插件版本 3.0.0-M5 成功绕过此问题,如here所述:

我们在 3.0.0-M5 版本中对插件进行了改进,因此您无需在依赖项中使用引擎。这种新方法避免在您的测试中使用引擎的内部代码,它使您只需调用 API

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。