JUnit4和JUnit5测试未在IntelliJ中运行

高兴的

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

相反,我收到以下错误:

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>
安舒尔·沙玛(Anshul Sharma)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Jupiter Junit5平台的老式引擎通过gradle配置按类别运行JUnit4测试

为什么要将现有测试从 JUnit4 转换为 Junit5?

JUnit5向后兼容JUnit4吗?

使用Maven在Eclipse中运行单个JUnit4测试

使用Spring Boot和JUnit5在Intellij中终止所有测试

测试未按JUnit5中的指定顺序运行

@RunWith未在JUnit5中编译

Maven不会运行JUnit5测试

在Kotlin和JUnit5中测试Spring Boot缓存

Junit5用的IntelliJ和摇篮

测试中的JUnit5断言计数

运行单元测试时运行的Junit5和Instrumentation测试

JUnit Jupiter(JUnit5)中的参数化测试执行

JUnit4使用测试套件在特定软件包中运行所有测试

在xtext环境中将JUnit4升级到JUnit5

@ Before / @ BeforeEach继承行为更改JUnit4 / JUnit5

从 JUnit4 迁移到 JUnit5 会在 @Autowired 存储库上引发 NullPointerException

从Junit4迁移到Junit5时,没有Runnable方法错误:

为什么Eclipse在应使用JUnit4时尝试使用JUnit5?

运行JUnit4测试时出现NullPointerException,带有MockitoHint“ Unused ...”和“ args OK?”

摇篮项目运行JUnit 5次测试中的IntelliJ

按指定顺序运行JUnit4测试类

安排Eclipse隔夜运行JUnit4测试

是否可以在Intellij Idea中更改junit4测试的默认类名称模板?

如何配置Maven插件以运行Spock和Junit5测试

如何在Android Studio 1.1中运行简单的JUnit4测试?

如何在JUnit4中按特定顺序运行测试方法?

如何运行基于 JUnit5 的测试套件?

在JUnit4测试中,在@Before和@After中访问对象的同一实例