2

I am new to IDEA and Gradle ecosystems, so please forgive me for asking basic questions, if any.

It's been close to 2 years since IntelliJ officially announced the support for JUnit 5. For some reason, IDEA still hates JUnit 5. If you follow the official instructions to add JUnit support, IDEA defaults to JUnit 4.12.

Anyway, after reading around I think I've correctly set up dependencies in my 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")
}

I have all of these in my classpath. Plus, I figured out myself to manually add opentest4j-1.1.0.jar in my classpath.

I followed these instructions to create a new run configuration for JUnit. The tests do run successfully, but along with test runner's output I get plenty of exceptions as follows. Am I doing something wrong?

"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

My tests look like this:

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)));
    }
}
Anurag Bhandari
  • 623
  • 2
  • 6
  • 20

3 Answers3

8

Turned out I had an 'extra' library in my classpath - org.junit.vintage:junit-vintage-engine. Removing it from classpath fixed the errors.

Anurag Bhandari
  • 623
  • 2
  • 6
  • 20
3

I got the same exceptions when running my tests even though my tests ran successfully. In my case, I was trying to run both JUnit4 and JUnit5 tests in the same project. This post helped me a lot.

At first, I copied the same configuration, but when I tried to run mvn install, the JUnit 5 tests were skipped. Changing the surefire.version to 2.22.0 fixed it. I removed the junit-platform-surefire-provider dependency because it is not needed anymore (please see this answer). Although, in my case, junit-jupiter-engine is needed else mvn install doesn't work.

Now, I can run my tests successfully and without warnings. Hope it helps someone else.

Community
  • 1
  • 1
tifa
  • 216
  • 2
  • 6
0

Specifying the JUnit engine in Gradle does the trick:

test {
    useJUnitPlatform {
        includeEngines 'junit-jupiter'
    }
}
jspetrak
  • 103
  • 1
  • 9