错误“带有 ID 'junit-vintage' 的 TestEngine 未能发现测试”与 Spring Boot 2.2

Iva*_*sco 60 spring-boot junit5

我有一个使用 Spring Boot 和 Junit 5 的简单应用程序:

  • 使用 Spring Boot 2.1(例如 2.1.8 或 2.1.12)时,我的单元测试运行顺利

  • 使用 Spring Boot 2.2(例如 2.2.2.RELEASE 或 2.3.2.RELEASE)时,我的单元测试失败并显示错误消息

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project XXX: There are test failures.
[ERROR]
[ERROR] Please refer to D:\Projets\workspace\XXX\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] There was an error in the forked process
[ERROR] TestEngine with ID 'junit-vintage' failed to discover tests
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
[ERROR] TestEngine with ID 'junit-vintage' failed to discover tests
[ERROR]         at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:656)
Run Code Online (Sandbox Code Playgroud)

我使用的是 Maven 3.6.1、JDK 1.8、JUnit 5.6.0 和 JUnit 平台 1.6.0。我排除了对junit:junitfrom的依赖spring-boot-starter-test,这样我的依赖树中就没有剩下的 JUnit 4 工件了。请注意,Spring Boot 2.2 和 2.3 都使用maven-surefire-plugin2.22.2,所以我的问题不是源于maven-surefire-plugin.

我应该坚持使用 Spring Boot 2.1以使我的单元测试正常工作吗?

在此先感谢您的帮助。

Iva*_*sco 91

我发现了错误。的依赖spring-boot-starter-test带来了对 的依赖junit-vintage-engine。必须排除后者:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • 这种依赖确实很麻烦。我已经有了这些排除。&lt;排除&gt; &lt;groupId&gt;junit&lt;/groupId&gt; &lt;artifactId&gt;junit&lt;/artifactId&gt; &lt;/排除&gt; &lt;排除&gt; &lt;groupId&gt;com.vaadin.external.google&lt;/groupId&gt; &lt;artifactId&gt;android-json&lt;/artifactId&gt; &lt;/排除&gt; (4认同)

小智 25

显式添加以下依赖项以升级junit-vintage-engine可解决此问题:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.7.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)


Sai*_*pta 21

最好的选择是在启用调试的情况下运行 Maven 测试:

例如mvn clean test -X。它将显示确切的失败原因。

就我而言,我尝试运行一些 Spock 测试,但未找到所需的 Groovy XML 依赖项(请参阅下面的堆栈跟踪)。当我显式添加 Groovy 依赖项时,问题得到了解决。

TestEngine with ID 'spock' failed to discover tests
org.junit.platform.commons.JUnitException: TestEngine with ID 'spock' failed to discover tests
        at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:111)
        at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:85)
        at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:92)
        at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:67)
        at org.apache.maven.surefire.junitplatform.TestPlanScannerFilter.accept(TestPlanScannerFilter.java:56)
        at org.apache.maven.surefire.api.util.DefaultScanResult.applyFilter(DefaultScanResult.java:102)
        at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.scanClasspath(JUnitPlatformProvider.java:147)
        at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:128)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:428)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
        at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:562)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:548)
Caused by: java.lang.NoClassDefFoundError: groovy/xml/MarkupBuilder
        at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
...
Run Code Online (Sandbox Code Playgroud)

  • 我非常同意你的观点!就我而言,我还检查了 Surefire 的转储文件,并在那里发现了另一个类的关于 NoClassDefFoundError 的臭名昭著的错误。你真的帮助了我。 (5认同)

Ali*_*lek 10

就我而言,问题是通过使 ide (IntelliJ) 缓存失效来解决的。

File > Invalidate Cache > Invalidate and Restart

在此输入图像描述


小智 9

我也有这个问题。我尝试了上述不同的解决方案,但没有任何效果。真正起作用的是 Intellij 的简单而可笑的重启。


Mir*_*ciu 8

在我的情况下(GRADLE 和 Spring Boot 2.xx),为 Vintage 添加排除工作

configurations {
    all {
        exclude(group = "junit", module = "junit")
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*ran 7

对于问题中报告的错误或以下错误,都与同一问题有关。

java.lang.NoClassDefFoundError: junit/runner/Version
Run Code Online (Sandbox Code Playgroud)

如果项目JUnit 4在依赖于 时排除或不包括,则会发生此错误spring-boot-starter-test。将spring-boot-starter-test依赖于junit-vintage-engine默认。要解决这个问题,要么我们必须排除junit-vintage-engine要么不应该依赖spring-boot-starter-test.

    testImplementation('org.springframework.boot:spring-boot-starter-test:2.2.2.RELEASE') {
        exclude group: 'junit'
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }

    testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.2')
    testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.2')
    testImplementation('org.junit.jupiter:junit-jupiter-params:5.5.2')

Run Code Online (Sandbox Code Playgroud)


Jas*_*ase 5

我通过使用Maven 依赖分析器并搜索“junit”修复了相同的错误。第一方依赖项(红条)引入了 json-simple,它引入了 4.10 junit 依赖项。只需排除 4.10 junit(右键单击 +排除)即可修复此问题。

Maven 助手的屏幕截图