xiang
xiang

Reputation: 1534

How to use JUnit 5 with build.gradle.kts and Kotlin?

It will be even better if we could use jigsaw module system.

Upvotes: 14

Views: 5320

Answers (3)

Mahozad
Mahozad

Reputation: 24532

A little more complete answer:

tasks.withType<Test> {
    useJUnitPlatform() // Enables JUnit Platform (JUnit 5 + JUnit 4)
}

dependencies {
    // Aggregator dependency that also brings JUnit 5 parameterized tests etc.
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
    // Include the Vintage engine to be able to run JUnit 4 tests as well
    // testImplementation("org.junit.vintage:junit-vintage-engine:5.10.0")
}

Upvotes: 0

Marc Philipp
Marc Philipp

Reputation: 1908

There's documentation and a sample project for using Gradle and JUnit 5.

If you want to use the Kotlin DSL you'll have to adapt it in the following way:

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

For Jigsaw, Gradle provides experimental support and there's a fork of the plugin that provides additional features.

Upvotes: 29

ordonezalex
ordonezalex

Reputation: 2745

Marc Philipp's answer works. Here is another approach:

val test: Test by tasks
test.useJUnitPlatform()

Upvotes: 4

Related Questions