Why do Gradle docs specify "junit-platform-launcher" for JUnit 5 tests?

The Testing in Java & JVM projects documentation suggests the following dependencies.

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

However, my testing shows that only junit-jupiter is required in order to run unit tests:

  • via the Gradle test task
  • in IntelliJ IDEA

Here’s my example build.gradle.kts.

plugins {
    java
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}

tasks.withType<Test>().configureEach {
    useJUnitPlatform()
}

What am I missing here? Why is junit-platform-launcher a suggested dependency, when it’s not required to run tests?

Maybe it should simply show testRuntimeOnly? Or maybe it was necessary but is not anymore? Or maybe it is just a doc bug?

I suggest you open an issue on GitHub, then the Gradle folks can have a look and clarify or fix it.

Good shout! Gradle team successfully poked.

2 Likes

I noticed this in Gradle 8 as well:

dependencies {
    …
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
    …
}

And did you read the response in the issue?
It is explained there and in the docs. :wink:

Why is there no version number in the dependency declaration for org.junit.platform:junit-platform-launcher?

Because it is not necessary and you cannot select a wrong version that way.
I didn’t check, but I guess the Jupiter dependency also brings in the platform BOM which then brings in the version for the launcher dependency.

I assume if you do not provide that dependency yourself, the runner (gradle or intellij) will their own runner version. I just ran into an issue after upgrading to spring boot 3.5.0 that those versions were misaligned. Adding the test runtime only fixed the issue for me

    testImplementation("org.junit.jupiter:junit-jupiter")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")

Before the error message was

OutputDirectoryProvider not available; probably due to unaligned versions of the junit-platform-engine and junit-platform-launcher jars on the classpath/module path.