Error “TestEngine with ID 'junit-vintage' failed to discover tests” with Spring Boot 2.2

*爱你&永不变心* 提交于 2021-02-07 11:14:38

问题


I have a simple app using Spring Boot and Junit 5:

  • When using Spring Boot 2.1 (e.g, 2.1.8 or 2.1.12), my unit tests run smoothly

  • When using Spring Boot 2.2 (e.g., 2.2.2.RELEASE or 2.3.2.RELEASE) my unit tests fail with error message

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

I am using Maven 3.6.1, JDK 1.8, JUnit 5.6.0 and JUnit platform 1.6.0. I exclude the dependency on junit:junit from spring-boot-starter-test, so that I have no JUnit 4 artifacts left in the dependency tree. Note that both Spring Boot 2.2 and 2.3 use maven-surefire-plugin 2.22.2, so my problem does not originate from any regression of the maven-surefire-plugin.

Should I stick to Spring Boot 2.1 in order to have my unit test working?

Thanks in advance for your help.


回答1:


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>



回答2:


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>



回答3:


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")
    }
}



回答4:


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




回答5:


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.




回答6:


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




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/59900637/error-testengine-with-id-junit-vintage-failed-to-discover-tests-with-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!