JUnit4和JUnit5测试未在IntelliJ中运行 [英] JUnit4 and JUnit5 tests not running in IntelliJ

查看:215
本文介绍了JUnit4和JUnit5测试未在IntelliJ中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use JUnit4 and JUnit5 tests in the same project in IntelliJ IDEA 2017.1.5. Until now, all tests were based on JUnit4. I added the jupiter, platform and vintage dependencies to my pom.xml (including the junit-platform-surefire-provider and junit-vintage-engine for the surefire plugin). Now, neither my example test for JUnit4 nor the one for JUnit 5 are executed.

相反,出现以下错误:

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用户指南中的建议,尽可能接近,但我可能错过了一些东西. 如何使两个测试正常运行?(当然还有我所有的现有测试)

I tried to follow the advice from the JUnit 5 User Guide as closely as possible but I probably missed something. How can I get both tests to run properly? (and all my existing tests, of course)

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>

推荐答案

使用以下版本的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依赖项.

And also use on version 5.0.0-M4 for all junit-jupiter dependencies.

这篇关于JUnit4和JUnit5测试未在IntelliJ中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆