Junitテストで異常が発生:Exception in thread"main"java.lang.NoSuchMethodError: org.junit.platform.commons.util.

6325 ワード

ユニットテスト中、テストに異常が発生しました
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)
pom.xml依存度は次のとおりです.
<dependencies>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
    <dependency>
        <groupId>org.junit.jupitergroupId>
        <artifactId>junit-jupiter-apiartifactId>
        <version>RELEASEversion>
    dependency>
dependencies>

問題の原因エラーはpom.xmlの依存で、コンソールの出力をよく見ると、IntelliJ IDEAがJUnit 5を使用して私のテスト例を実行しようとしていることがわかります.
at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
pom.xmlを通して、私はJUnit 4を使いたいことを発見しました.12テスト例を実行し、pom.xmlを見て、junit-jupiter-apiという依存がこのエラーを引き起こすことを発見しました.
<dependency>
    <groupId>org.junit.jupitergroupId>
    <artifactId>junit-jupiter-apiartifactId>
    <version>RELEASEversion>
dependency>

このモジュールはJUnit 5専用に設計されているため、https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
ソリューション
1、削除pom.xmlorg.junit.jupiter依存2、Reimport All Maven Project
原文(英語)住所:https://stackoverflow.com/questions/45004453/cannot-run-tests-intellij-spring-project-error-java-lang-nosuchmethoderror