我是IDEA和Gradle生态系统的新手,所以请原谅我提出基本问题。
自IntelliJ officially announced对JUnit 5的支持以来已经快两年了。由于某些原因,IDEA仍然讨厌JUnit5。如果您按照官方说明添加JUnit支持,则IDEA默认为JUnit 4.12。 / p>
无论如何,阅读完之后,我认为我已经在build.gradle中正确设置了依赖项:
dependencies {
testCompile("org.junit.platform:junit-platform-launcher:1.2.0")
testCompile("org.junit.jupiter:junit-jupiter-engine:5.2.0")
testCompile("org.junit.vintage:junit-vintage-engine:5.2.0")
}
所有这些都在我的类路径中。另外,我发现自己可以在类路径中手动添加 opentest4j-1.1.0.jar 。
我遵循these instructions为JUnit创建新的运行配置。测试确实可以成功运行,但是随着测试运行程序的输出,我得到了很多异常,如下所示。我在做错什么吗?
"C:\Program Files\Java\jdk-10.0.1\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\lib\idea_rt.jar=57664:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\plugins\junit\lib\junit-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\plugins\junit\lib\junit5-rt.jar;C:\Users\anurag.x.bhandari\IdeaProjects\HackerRankCodingInterview\out\test\Solutions;C:\Users\anurag.x.bhandari\IdeaProjects\HackerRankCodingInterview\out\production\Solutions;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.jupiter\junit-jupiter-api\5.2.0\9cd901df48d88d8e605a6ccb2c3f140c92db6bf2\junit-jupiter-api-5.2.0.jar;C:\Users\anurag.x.bhandari\IdeaProjects\HackerRankCodingInterview\libs\opentest4j-1.1.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.platform\junit-platform-launcher\1.2.0\ea230ba8c0b71943d4dd2bb215ca29041c89966e\junit-platform-launcher-1.2.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.jupiter\junit-jupiter-engine\5.2.0\de87318ccd3dfa1a98ebfef792d362776f1914de\junit-jupiter-engine-5.2.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.platform\junit-platform-commons\1.2.0\dbce1d822d3dc6c61703b340cd79018518685451\junit-platform-commons-1.2.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.vintage\junit-vintage-engine\5.2.0\ff11a5d0674df5e2264e50fe3f8a3485a4399ccb\junit-vintage-engine-5.2.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.platform\junit-platform-engine\1.2.0\35fa3529ce843ada1a10b0909ccb4a8148ee638d\junit-platform-engine-1.2.0.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 @w@C:\Users\anurag.x.bhandari\AppData\Local\Temp\idea_working_dirs_junit.tmp @C:\Users\anurag.x.bhandari\AppData\Local\Temp\idea_junit.tmp -socket57663
Jun 21, 2018 9:26:48 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoClassDefFoundError: org/junit/runners/model/RunnerBuilder
at org.junit.vintage.engine.discovery.VintageDiscoverer.<init>(VintageDiscoverer.java:35)
at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:61)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:82)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:52)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.runners.model.RunnerBuilder
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 9 more
Jun 21, 2018 9:26:49 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoClassDefFoundError: org/junit/runners/model/RunnerBuilder
at org.junit.vintage.engine.discovery.VintageDiscoverer.<init>(VintageDiscoverer.java:35)
at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:61)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.runners.model.RunnerBuilder
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 9 more
Expected :<1>
Actual :<5>
<Click to see difference>
org.opentest4j.MultipleFailuresError: Multiple Failures (1 failure)
array contents differ at index [0], expected: <1> but was: <5>
at org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:79)
at org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:45)
at org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:39)
at org.junit.jupiter.api.Assertions.assertAll(Assertions.java:1060)
at com.anuragbhandari.hackerrank.CorrectnessTests.checkLeftRotationOutput(ArrayLeftRotationTests.java:22)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:513)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:170)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:166)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:113)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:58)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:113)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(HierarchicalTestExecutor.java:121)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(HierarchicalTestExecutor.java:121)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:55)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
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
我的测试如下:
package com.anuragbhandari.hackerrank;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@Nested
@DisplayName("given a valid array of integers and a rotation count")
class CorrectnessTests {
@Test
@DisplayName("perform the given number of left rotations and return the correct output")
void checkLeftRotationOutput() {
int[] arrayOne = new int[] {1, 2, 3, 4, 5};
int arrayOneRotations = 4;
int[] arrayOneRotated = new int[] {5, 1, 2, 3, 4};
int[] arrayTwo = new int[] {41, 73, 89, 7, 10, 1, 59, 58, 84, 77, 77, 97, 58, 1, 86, 58, 26, 10, 86, 51};
int arrayTwoRotations = 10;
int[] arrayTwoRotated = new int[] {77, 97, 58, 1, 86, 58, 26, 10, 86, 51, 41, 73, 89, 7, 10, 1, 59, 58, 84, 77};
assertAll(() -> assertArrayEquals(arrayOneRotated, ArrayLeftRotation.rotLeft(arrayOne, arrayOneRotations)),
() -> assertArrayEquals(arrayTwoRotated, ArrayLeftRotation.rotLeft(arrayTwo, arrayTwoRotations)));
}
}
答案 0 :(得分:2)
原来,我在类路径中有一个“额外”库- org.junit.vintage:junit-vintage-engine 。从类路径中删除它可以修复错误。
答案 1 :(得分:1)
即使测试成功运行,我在运行测试时也会遇到相同的异常。就我而言,我试图在同一项目中运行JUnit4和JUnit5测试。 This post对我有很大帮助。
起初,我复制了相同的配置,但是当我尝试运行时
mvn install
,跳过了JUnit 5测试。将surefire.version
更改为2.22.0可以解决此问题。我删除了junit-platform-surefire-provider
依赖项,因为不再需要它(请参见this answer)。尽管就我而言,junit-jupiter-engine
是必需的,否则mvn install
不起作用。
现在,我可以成功运行测试,而不会发出警告。希望对别人有帮助。
答案 2 :(得分:0)
在Gradle中指定JUnit引擎可以达到目的:
test {
useJUnitPlatform {
includeEngines 'junit-jupiter'
}
}