Spring Boot 2.2中的错误“ TestEngine with ID‘ junit-old’未能发现测试”

我有一个使用 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)

我使用的是 Maven 3.6.1、 JDK 1.8、 JUnit 5.6.0和 JUnit 平台1.6。我从 spring-boot-starter-test中排除了对 junit:junit的依赖关系,因此在依赖关系树中没有留下 JUnit 4构件。请注意,Spring Boot 2.2和2.3都使用 maven-surefire-plugin2.22.2,因此我的问题不是来自 maven-surefire-plugin的任何回归。

我是否应该坚持使用 SpringBoot2.1来使我的单元测试正常工作?

先谢谢你的帮助。

149702 次浏览

I found the error. The dependency on spring-boot-starter-test brings a dependency on junit-vintage-engine. The latter must be excluded:

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

In my case (GRADLE and Spring Boot 2.x.x), adding exclusion for vintage worked

configurations {
all {
exclude(group = "junit", module = "junit")
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}

For the reported error in the question or the below error, both relate to the same problem.

java.lang.NoClassDefFoundError: junit/runner/Version

This error occurs if the project excludes or not include JUnit 4 when it depends on spring-boot-starter-test. The spring-boot-starter-test depends on junit-vintage-engine by default. To resolve this issue either we have to exclude junit-vintage-engine Or should not depend on 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')


Excluding the dependencies is not an option for us. Because with that we don't have our tests run. So our solution was to downgrade the JUnit version:

<!--
junit-vintage-engine 5.5.2 version does not support junit 4.13.1.
If we try to use it, a parsing error is thrown.
So we downgrade JUnit, so that junit-vintage-engine will handle it.
-->
<version.legacy.junit>4.13</version.legacy.junit>

Using Spring Boot 2.3.4 seems to be working.

Adding the following dependency explicitly to upgrade junit-vintage-engine resolves the issue:

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.7.0</version>
</dependency>

What solve my problem in this case, is changing Settings of IntelliJ, to use Gradle not Intellij as Gradle builder.

enter image description here

Similar issue can be observed for "junit-jupiter" test engine, in case of running tests located inside java9 named modules (also for Spring Boot app). The message is the same without clue about the reason:

TestEngine with ID "junit-jupiter" failed to discover tests

The problem in this case is that one of your test classes requires named java9 module which is not required in your module-info.java.

To confirm this go to that particular module and execute build with -X option to enable debugging stacktraces. After that you will get more detailed information on the problem:

    [ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
[ERROR] TestEngine with ID 'junit-jupiter' failed to discover tests
[ERROR] org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
(...)
[ERROR] Caused by: org.junit.platform.commons.JUnitException: ClassSelector [className = 'x.y.z.YourTest'] resolution failed
[ERROR]         at org.junit.platform.launcher.listeners.discovery.AbortOnFailureLauncherDiscoveryListener.selectorProcessed(AbortOnFailureLauncherDiscoveryListener.java:39)
(...)
[ERROR] Caused by: java.lang.IllegalAccessError: class x.y.z.YourTest (in module com.your.module) cannot access class com.fasterxml.jackson.core.JsonProcessingException (in module com.fasterxml.jackson.core) because module com.bnymellon.rei.data.exchange.au
th.service does not read module com.fasterxml.jackson.core

As the message says jackson.core is not available and needs to be added to your module descriptor module-info.java:

open module com.your.module {
(...)
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
}
                                                                                                                         

NOTE: In example above you can see also jackson.databind added as subsequent exceptions (after you rerun maven build) would most probably suggest that this one is missing as well.

I had this issue as well. I've tried different solutions mentioned above, but nothing worked. The one that did work was the simple and ridiculous restart of Intellij.

In my case in gradle this error was caused by having

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${jUnitVersion}"

instead of

testImplementation "org.junit.jupiter:junit-jupiter-engine:${jUnitVersion}"

my solution removed following dependency:

<dependency>
<groupId>junit-addons</groupId>
<artifactId>junit-addons</artifactId>
<scope>test</scope>
</dependency>

I ran into this situation with a project that worked fine a few months ago, but when I went back the unit tests no longer worked within IntelliJ. I restarted IntelliJ, no change, updated IntelliJ and "Wala" error went away.

Already had the exclusion in my pom for junit-vintage-engine, but was encountering the issue. Did invalidate cache and restart in IntelliJ to make it work.

Another reason for this error message could be a mixed junit-version set:

enter image description here

After consistently switching from junit 5.6.2 to 5.7.2 fixes the error for me.

I fixed the same error by using the Maven dependency analyzer and searching for 'junit'. A 1st party dependency (the red bar) was bringing in json-simple which was bringing in a 4.10 junit dependency. Simply excluding the 4.10 junit (right-click + Exclude) fixed it.

screenshot of maven helper

For me nothing from above worked. However, adding the latest version of JUnit from maven solved the issue for me.

The best option is to run the Maven test with debugging enabled:

e.g. mvn clean test -X. It will show the exact failure reason.

In my case, I was trying to run some Spock tests and the required Groovy XML dependency was not found (see stack trace below). It was solved when I added the Groovy dependency explicitly.

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

In my case, the problem was solved by invalidating idea (IntelliJ) cache.

File > Invalidate Cache > Invalidate and Restart

enter image description here

In case anyone is still having this issue, the above did not work for me. My solution involved using the latest spring boot test dependency, and then including the class name in the spring boot test annotation as shown below. I removed all my previous junit exclusions and explicit dependencies.

@SpringBootTest(classes = {testclass.class})

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.6.2</version>
</dependency>

Nothing worked for me in the previous answers. But I removed the "reuseForks" in configuration of maven surefire plugin and upgraded it from 2.22.0 to 2.22.2.

I had excluded the test from the IntelliJ compilation by mistake.

This worked for me: Preferences > Build, Execution, Deployment > Compiler > Excludes and then removing the excluded test from the list.