Maven JUnit4和JUnit5测试未在IntelliJ中运行
我试图在IntelliJ IDEA 2017.1.5的同一个项目中使用JUnit4和JUnit5测试。到目前为止,所有测试都基于JUnit4。我在我的pom.xml中添加了Maven JUnit4和JUnit5测试未在IntelliJ中运行,maven,intellij-idea,junit,junit4,junit5,Maven,Intellij Idea,Junit,Junit4,Junit5,我试图在IntelliJ IDEA 2017.1.5的同一个项目中使用JUnit4和JUnit5测试。到目前为止,所有测试都基于JUnit4。我在我的pom.xml中添加了jupiter、platform和vintage依赖项(包括用于surefire插件的junit platform surefire提供程序和junit Vitage引擎)现在,我的JUnit4示例测试和JUnit5示例测试都没有执行。 相反,我得到了以下错误: Exception in thread "main" java.
jupiter
、platform
和vintage
依赖项(包括用于surefire插件的junit platform surefire提供程序和junit Vitage引擎
)现在,我的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.
我试着尽可能地听从老师的建议,但我可能错过了什么如何使这两个测试正常运行?(当然还有我现有的所有测试)
JUnit4测试类
JUnit5测试类
pom.xml(相关部分)
org.apache.maven.plugins
maven surefire插件
2.19.1
src/测试
**/*WebappTest.java
org.junit.platform
junit平台surefire提供程序
1.0.0-M5
org.junit.jupiter
朱尼特木星发动机
5.0.0-M5
org.junit.vintage
朱尼特老式发动机
4.12.0-M5
org.apache.maven.plugins
maven编译器插件
3.5.1
1.8
1.8
UTF-8
org.junit.jupiter
朱尼特木星发动机
5.0.0-M5
测试
org.junit.jupiter
JUnitJupiter api
5.0.0-M5
测试
org.junit.vintage
朱尼特老式发动机
4.12.0-M5
测试
朱尼特
朱尼特
4.12
测试
使用以下版本的junit jupiter api
:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M4</version>
<scope>test</scope>
</dependency>
org.junit.jupiter
JUnitJupiter api
5.0.0-M4
测试
也可在版本
5.0.0-M4
上用于所有junit jupiter
依赖项。junit jupiter用于junit 5,junit vintage用于旧版本,
如果您只使用junit 5,那么只使用junit jupiter就足够了,
如果您已经实现了JUnit 4或任何较早版本的实现,那么您必须同时使用JUnit jupiter和JUnit vintage依赖项
注意
JUnit 5的体系结构还支持同时运行多个测试引擎:您可以将JUnit Vintage测试引擎与几乎任何其他与JUnit 5兼容的测试引擎一起运行。谢谢!这与IntelliJ目前只支持特定的JUnit5里程碑有关,对吗?对。请参阅注释中的详细信息:Gradle的这种变通方法(添加testRuntime依赖项)不适用于Android项目,因为Gradle Android插件不支持testRuntime关键字仅使用maven获得完全相同的错误-但我看到您控制着maven surefire插件依赖项,否则,就像我的问题一样,它引入了较旧版本的JUnit5
package com.glaed.util;
import org.junit.jupiter.api.Test;
class JUnit5Test {
@Test
void helloJU5test() {
System.out.println("Hello JUnit5!");
}
}
<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>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M4</version>
<scope>test</scope>
</dependency>