Gradle 4.6 and JUnit 5

Hi

I used JUnit 5 for my unit-tests with Gradle 4.5 and earlier ( I use Spring Boot plugin so no versions are specified):

dependencies {
    testCompile 'org.junit.jupiter:junit-jupiter-api'
    testRuntime 'org.junit.jupiter:junit-jupiter-engine'
}

In Gradle 4.6 release notes there was a statement:

Gradle now provides native support for JUnit Jupiter/Vintage Engine on top of JUnit Platform. To enable JUnit Platform support, you just need to add one line to your build.gradle:

test {
useJUnitPlatform()
}

So I tried to change my build.gradle to:

dependencies {
    testCompile 'org.junit.jupiter:junit-jupiter-api' 
}

test {
    useJUnitPlatform()
}

However I’ve got exception:

:proj1:junitPlatformTestorg.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
at org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:297)
at org.junit.platform.launcher.core.DefaultLauncher.(DefaultLauncher.java:55)

Please, advise

You didn’t add any test engine, as the error message says. You only added jupiter-api, but not jupiter-engine.

1 Like

Yes. if I add jupiter-engine dependency the tests will be run successfully.

So what is the purpose of useJUnitPlatform() then? It seems that it changes nothing as it’s possible to run JUnit 5 tests without using it.

It is not possible to run JUnit 5 tests without useJunitPlatform(). The default is the JUnit 4 framework. Maybe you previously used the org.junit.platform.gradle.plugin, which is different from the new built-in support.

That’s right. I used ‘org.junit.platform.gradle.plugin’ plugin for running JUnit 5 tests.
If I remove this plugin and switch to useJUnitPlatform() configuration for tests then tests are executed successfully.

However the need to add jupiter-engine dependency seems redundant to me as it’s default engine for running JUnit 5 tests.

We don’t prescribe any engine. The same goes for JUnit 4. It’s up to the user to pick an engine and its version.