Миграция JUnit 5 — как решить java.lang.Исключение ClassNotFoundException: org.junit.jupiter.api.расширение.Рефлексивный инвокационный контекст

#java #junit4 #junit5

Вопрос:

Когда я запускаю некоторые тесты JUnit 5(в том же проекте также есть тесты JUnit 4, так как мы находимся в процессе перехода на JUnit 5), я вижу эту ошибку:

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

Как это решить?

Ответ №1:

Убедитесь, что у вас есть эти зависимости в вашем pom.xml :

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

Для JUnit 4:

  • июнь:4.12
  • org.junit.vintage:движок junit-vintage (он поддерживает только JUnit 4.12 )

И если вы хотите использовать @ParameterizedTest:

  • org.junit.jupiter:junit-jupiter-api
  • org.junit.юпитер:юнит-юпитер-парам

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>-->