Search code examples
eclipsercptychojunit5

How to build Junit5 tests w/ Maven Tycho


I want to introduce Junit5 Jupiter to an old RCP test fragment. We build our RCP features w/ Maven Tycho. Since version 1.2, Tycho is supposed to support JUnit5 tests.

All I get is (when I run mvn clean install)

org.apache.maven.surefire.util.SurefireReflectionException: java.util.ServiceConfigurationError: org.junit.platform.engine.TestEngine: Provider org.junit.jupiter.engine.JupiterTestEngine not a subtype
    at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:135)
    at org.apache.maven.surefire.booter.SurefireReflector.instantiateProvider(SurefireReflector.java:276)
    at org.apache.maven.surefire.booter.ProviderFactory.createProvider(ProviderFactory.java:113)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:81)
    at org.eclipse.tycho.surefire.osgibooter.OsgiSurefireBooter.run(OsgiSurefireBooter.java:107)
    at org.eclipse.tycho.surefire.osgibooter.AbstractUITestApplication.runTests(AbstractUITestApplication.java:44)
    at org.eclipse.e4.ui.internal.workbench.swt.E4Testable.lambda$0(E4Testable.java:73)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.util.ServiceConfigurationError: org.junit.platform.engine.TestEngine: Provider org.junit.jupiter.engine.JupiterTestEngine not a subtype
    at java.util.ServiceLoader.fail(ServiceLoader.java:239)
    at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at org.junit.platform.launcher.core.DefaultLauncher.validateUniqueIds(DefaultLauncher.java:63)
    at org.junit.platform.launcher.core.DefaultLauncher.<init>(DefaultLauncher.java:58)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:59)
    at org.junit.platform.surefire.provider.JUnitPlatformProvider.<init>(JUnitPlatformProvider.java:85)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:131)
    ... 7 more

These are my dependencies from the Manifest.MF:

 org.junit.jupiter.api;bundle-version="5.0.0",
 org.junit.jupiter.engine;bundle-version="5.0.0",
 org.junit.jupiter.params;bundle-version="5.0.0",
 org.junit.jupiter.migrationsupport;bundle-version="5.0.0",
 org.junit.vintage.engine;bundle-version="4.12.0",
 org.hamcrest.core;bundle-version="1.3.0",
 org.junit;bundle-version="4.12.0",
 org.opentest4j,
 org.junit.platform.commons;bundle-version="1.0.0",
 org.junit.platform.engine;bundle-version="1.0.0",
 org.junit.platform.launcher;bundle-version="1.0.0",
 org.junit.platform.runner;bundle-version="1.0.0",
 org.junit.platform.suite.api;bundle-version="1.0.0"

I added those dependencies mostly by trial and error. I know I need those platform bundles if I want to run the J5 tests in Eclipse, too. In Eclipse, both the old and the new tests work fine.

The junit 4.12 dependency is there because otherwise all those old unit tests won't compile anymore.

Is there an example on how to get Junit5 tests going using Tycho? Maybe I am googling the wrong stuff ;)


Solution

  • See https://wiki.eclipse.org/Tycho/How_Tos/JUnit5

    The trick seems to be not to add j5 as dependencies but by import-package.

    This worked for me.