【问题标题】:JUnit 5 migration - how to solve java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.ReflectiveInvocationContextJUnit 5 迁移 - 如何解决 java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.ReflectiveInvocationContext
【发布时间】:2021-05-19 12:32:14
【问题描述】:

当我运行一些 JUnit 5 测试时(同一个项目中也有 JUnit 4 测试,因为我们正在向 JUnit 5 迁移),我看到这个错误:

No tests were executed
...
Caused by:
java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.ReflectiveInvocationContext

如何解决?

【问题讨论】:

    标签: java junit4 junit5


    【解决方案1】:

    确保您的 pom.xml 中有这些依赖项:

    • org.junit.jupiter:junit-jupiter-engine

    对于 JUnit 4:

    • junit:4.12+
    • org.junit.vintage:junit-vintage-engine(仅支持 JUnit 4.12+)

    如果你想使用@ParameterizedTest:

    • org.junit.jupiter:junit-jupiter-api
    • org.junit.jupiter:junit-jupiter-params

    org.junit.platform:junit-platform-launcher 不需要,即使您想在 Intellij 中运行测试。也许你可以试试不带它。

            <!-- junit 5 -->
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit5.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- junit 5 parameterized tests -->
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-params</artifactId>
                <version>${junit5.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit5.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- for compatibility for JUnit 4. JUnit vintage needs 4.12+ -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <!-- for compatibility for JUnit 4 -->
            <dependency>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
                <version>${junit5.version}</version>
                <scope>test</scope>
            </dependency>
    
            <!-- for IDE support only(running tests from IDE) -->
    <!--        <dependency>-->
    <!--            <groupId>org.junit.platform</groupId>-->
    <!--            <artifactId>junit-platform-launcher</artifactId>-->
    <!--            <version>1.7.1</version>-->
    <!--            <scope>test</scope>-->
    <!--        </dependency>-->
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多