JUnit4和JUnit5测试在IntelliJ中无法运行

12
我正在尝试在IntelliJ IDEA 2017.1.5中的同一项目中使用JUnit4和JUnit5测试。到目前为止,所有测试都基于JUnit4。我向我的pom.xml添加了jupiterplatformvintage依赖项(包括junit-platform-surefire-providerjunit-vintage-engine用于surefire插件)。现在,我的JUnit4示例测试和JUnit5测试都没有执行。

相反,我得到以下错误:

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)

Process finished with exit code 1
Empty test suite.

我尽可能地遵循了JUnit 5 用户指南中的建议,但我可能漏掉了什么。 我该如何使这两个测试都正常运行? (当然还要包括我所有已有的测试)

JUnit 4 测试类

package com.glaed.util;

import org.junit.Test;

public class JUnit4Test {

  @Test
  public void helloJUnit4Test() {
    System.out.println("Hello JUnit4!");
  }

}

JUnit 5测试类

package com.glaed.util;

import org.junit.jupiter.api.Test;

class JUnit5Test {

  @Test
  void helloJU5test() {
    System.out.println("Hello JUnit5!");
  }
}

pom.xml(相关部分)

    <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <testSourceDirectory>src/test</testSourceDirectory>
                    <excludes>
                        <exclude>**/*WebappTest.java</exclude>
                    </excludes>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M5</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.0-M5</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>4.12.0-M5</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<dependencies>

    <!-- JUNIT5 & JUPITER -->

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.0.0-M5</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.0.0-M5</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>4.12.0-M5</version>
        <scope>test</scope>
    </dependency>

    <!-- JUnit 4 -->

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

</dependencies>

只使用Maven时我也遇到了完全相同的错误 - 但是后来我发现你正在控制Maven Surefire插件的依赖项,否则 - 就像我的问题一样 - 它会拉取JUnit5的旧版本。 - Adam
2个回答

11

请使用以下版本的junit-jupiter-api

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

同时,在所有junit-jupiter依赖项中也要使用版本为5.0.0-M4的。


谢谢!这与IntelliJ目前仅支持特定的JUnit5里程碑有关,对吗? - glaed
5
好的。请查看http://junit.org/junit5/docs/current/user-guide/#running-tests-ide-intellij-idea上的详细信息。 - Sormuras
注意:针对Gradle的这种解决方法(添加testRuntime依赖项)不适用于Android项目,因为Gradle Android插件不支持testRuntime关键字。 - Alix

0

junit-jupiter适用于JUnit 5,junit-vintage适用于旧版本, 如果您仅使用junit 5,则只需要junit-jupiter, 如果您已经实现了JUnit 4或任何早期版本的实现,则必须同时使用junit-jupiter和junit-vintage依赖项。

注意 JUnit 5的架构还支持同时运行多个测试引擎:您可以将JUnit Vintage测试引擎与几乎任何与JUnit 5兼容的其他测试引擎一起运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接