org.junit.platform.commons.JUnitException:ID 为 'junit-jupiter' 的 TestEngine 未能发现测试

org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests

我想在 Gradle 项目中实施 Junit 5 测试。我试过这个:

Gradle配置:

plugins {
    id 'org.springframework.boot' version '2.5.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'test'
version = '0.0.1'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2020.0.4")
}

dependencies {
    ...............
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

联合测试:

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.util.concurrent.TimeUnit;

public class GeneratePdf {

    @DisplayName("Test MessageService.get()")
    @Test
    @Timeout(value = 60, unit = TimeUnit.SECONDS)
    public void generatePdfFileTes() throws InterruptedException
    {
        System.out.println("test!");
    }

}

当我 运行 代码时出现错误:

Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:160)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverSafely(EngineDiscoveryOrchestrator.java:134)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:108)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:80)
    at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:110)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.junit.platform.commons.JUnitException: MethodSelector [className = 'org.merchant.poc.GeneratePdf', methodName = 'generatePdfFileTes', methodParameterTypes = ''] resolution failed
    at org.junit.platform.launcher.listeners.discovery.AbortOnFailureLauncherDiscoveryListener.selectorProcessed(AbortOnFailureLauncherDiscoveryListener.java:39)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:102)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.run(EngineDiscoveryRequestResolution.java:82)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolver.resolve(EngineDiscoveryRequestResolver.java:113)
    at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:46)
    at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
    at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:152)
    ... 13 more
Caused by: org.junit.platform.commons.PreconditionViolationException: Could not load class with name: org.merchant.poc.GeneratePdf
    at org.junit.platform.engine.discovery.MethodSelector.lambda$lazyLoadJavaClass[=14=](MethodSelector.java:157)
    at org.junit.platform.commons.function.Try$Failure.getOrThrow(Try.java:335)
    at org.junit.platform.engine.discovery.MethodSelector.lazyLoadJavaClass(MethodSelector.java:156)
    at org.junit.platform.engine.discovery.MethodSelector.getJavaClass(MethodSelector.java:135)
    at org.junit.jupiter.engine.discovery.MethodSelectorResolver.resolve(MethodSelectorResolver.java:73)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.lambda$resolve(EngineDiscoveryRequestResolution.java:146)
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197)
    at java.base/java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1602)
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129)
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
    at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:185)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:125)
    at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:91)
    ... 18 more
Caused by: java.lang.ClassNotFoundException: org.merchant.poc.GeneratePdf
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    at org.junit.platform.commons.util.ReflectionUtils.lambda$tryToLoadClass(ReflectionUtils.java:829)
    at org.junit.platform.commons.function.Try.lambda$call[=14=](Try.java:57)
    at org.junit.platform.commons.function.Try.of(Try.java:93)
    at org.junit.platform.commons.function.Try.call(Try.java:57)
    at org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:792)
    at org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:748)
    ... 34 more

您知道如何解决这个问题吗?

编辑:

import org.junit.jupiter.api.Test;

public class InvoiceTests {

    @Test
    public void generateTest()
    {
        System.out.println("test!");
    }

}

我试过了,但还是不行。

GeneratePdf 与测试 类 的默认名称模式不匹配。默认模式是 Test*|*Test|*Tests.

您可以使用

在 Gradle 文件中更改它
test {
  useJUnitPlatform()
  include '**/*Pdf'
}

您遵循的项目结构是什么?我记得刚开始的时候遇到过类似的问题,所以基本上我所做的就是遵循这个结构:

project-dir
 -- src
     -- main
        -- java
            -- classes
     -- test
        -- java
            -- test-classes

除了上述结构之外,请确保您以正确的格式命名您的测试 类 by @johanneslink

我将 junit 的版本从 5.7.0 升级到 5.8.2 时遇到了这个问题。 尝试使用 org.junit.jupiter:junit-jupiter-apiorg.junit.jupiter:junit-jupiter-engine

版本 5.7.0

我遇到了同样的错误,因为我将我的集成测试 class 命名为 XyzIT,post 将其重命名为 XyzIntegrationTest,它开始工作了。

环境:

  • IntelliJ
  • Java 17

将我的项目从 JDK 11 升级到 JDK 17 并将 junit-jupiter-api 从 5.7.0 升级到 5.8.2 后,我遇到了同样的问题。

在我的项目中,工作版本是 5.7.2

关注这里的问题:Github issue