【问题标题】:java.lang.NoSuchMethodError when run test with Junit 5使用 Junit 5 运行测试时出现 java.lang.NoSuchMethodError
【发布时间】:2017-09-23 21:54:45
【问题描述】:

我创建了一个简单的测试来尝试 Junit 5:

import org.junit.jupiter.api.Test;
public class MyTest {

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

这是我使用的依赖:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0</version>
            <scope>test</scope>
        </dependency>

堆栈跟踪是下一个:

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:30)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

任何想法出了什么问题?

【问题讨论】:

  • 堆栈跟踪消息的更多行会有所帮助
  • 请分享@Test注解的导入

标签: java junit5


【解决方案1】:

我看到您使用的 IntelliJ 仅支持特定版本的 jUnit5。

Junit5 - running tests in IntelliJ

尝试例如

<version>5.0.0-M4</version>

【讨论】:

    【解决方案2】:

    除了依赖之外,您是否还在pom.xml 中配置了JUnit5 插件?它应该看起来像这样(取自the docs):

    ...
    <build>
        <plugins>
            ...
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    ...
    

    【讨论】:

    • 我尝试添加插件。只有当我运行 mvn clean install 时才有帮助。但是,如果我右键单击测试然后选择“运行”,它仍然失败并出现相同的错误。事实证明,原因在于 Intellij idea 的特定依赖版本。但无论如何谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    相关资源
    最近更新 更多