JUnit4 and JUnit5 tests not running in IntelliJ
我试图在IntelliJ IDEA 2017.1.5的同一项目中使用JUnit4和JUnit5测试。到目前为止,所有测试都基于JUnit4。我在pom.xml中添加了
相反,出现以下错误:
1 2 3 4 5 6 7 8 9 10 | 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测试类
1 2 3 4 5 6 7 8 9 10 11 12 | package com.glaed.util; import org.junit.Test; public class JUnit4Test { @Test public void helloJUnit4Test() { System.out.println("Hello JUnit4!"); } } |
JUnit 5测试类
1 2 3 4 5 6 7 8 9 10 11 | package com.glaed.util; import org.junit.jupiter.api.Test; class JUnit5Test { @Test void helloJU5test() { System.out.println("Hello JUnit5!"); } } |
pom.xml(相关部分)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> 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> junit-platform-surefire-provider</artifactId> <version>1.0.0-M5</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>5.0.0-M5</version> </dependency> <dependency> <groupId>org.junit.vintage</groupId> junit-vintage-engine</artifactId> <version>4.12.0-M5</version> </dependency> </dependencies> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> 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> 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> junit-jupiter-api</artifactId> <version>5.0.0-M5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> junit-vintage-engine</artifactId> <version>4.12.0-M5</version> <scope>test</scope> </dependency> <!-- JUnit 4 --> <dependency> <groupId>junit</groupId> junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> |
使用以下版本的
1 2 3 4 5 6 | <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-api</artifactId> <version>5.0.0-M4</version> <scope>test</scope> </dependency> |
并且还用于所有
junit-jupiter用于JUnit 5,junit-vintage用于旧版本,
如果仅使用junit 5,则仅junit-jupiter就足够了,
并且如果您已经实现了JUnit 4或任何较旧版本的实现,则必须同时使用junit-jupiter和junit-vintage依赖项。
注意
JUnit 5的体系结构还支持同时运行多个测试引擎:您可以将JUnit Vintage测试引擎与几乎任何与JUnit 5兼容的其他测试引擎一起运行。