Friday, December 9, 2016

Playing with JUnit 5 and IntelliJ IDEA 2016.3

    I've been recently playing with JUnit 5 M3 and IntelliJ IDEA 2016.3. I managed to execute JUnit 5 tests from within IntelliJ IDEA and Maven. I decided to share it with you because it was not trivial.

IntelliJ IDEA 2016.3:
  • you need to add the following dependencies to you pom.xml file:
Without org.junit.platform:junit-platform-launcher dependency you will receive the following exception:
org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
INFO: Discovered TestEngines with IDs: [junit-jupiter]
Exception in thread "main" java.lang.NoSuchMethodError:
    at org.junit.platform.launcher.core.DefaultLauncher.registerTestExecutionListeners(
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
    at com.intellij.rt.execution.junit.JUnitStarter.main(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at com.intellij.rt.execution.application.AppMain.main( 
This is due to the fact that IntelliJ 2016.3 JUnit 5 test runner is based on M2 version (it includes M2 dependencies unless you specify differently in your pom.xml) whereas we tried to use M3 version. The method:
    org.junit.platform.launcher.core.DefaultLauncher.registerTestExecutionListeners -> M2
tried to call:
public static  T notNull(T object, String message) throws PreconditionViolationException -> M3
// it lives in org.junit.platform:junit-platform-commons which is a transitive dependency of
// org.junit.jupiter:junit-jupiter-engine 
However, DefaultLauncher M2 was compiled against:
public static Object[] notNull(Object[] objects, String message) throws PreconditionViolationException -> M2
So despite the fact there is no compilation error - there is the lack of binary compatibility. The cure was to add org.junit.platform:junit-platform-launcher:1.0.0-M3 explicitly. Thanks to that DefaultLauncher M3 is used by IntelliJ 2016.3 JUnit 5 test runner and it calls Preconditions M3. You can also wait for IntelliJ IDEA 2016.3.1 or IntelliJ IDEA 2017.1 because the problem was fixed in these versions.

  • you can use the same dependencies as for IntelliJ but you also need to add a plugin to your pom.xml:  
P.S. This is the last post in 2016. Happy New Year and stay tuned for the upcoming entries in 2017!

1 comment :