12

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

相反,我收到以下错误:

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>
4

2 回答 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>

并且还5.0.0-M4对所有junit-jupiter依赖项使用版本。

于 2017-07-11T16:36:51.703 回答
0

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

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

于 2020-05-27T05:02:53.133 回答