androidengineers.Book a session

Testing in Kotlin

JUnit 5 with Kotlin

article15 minHard

Align JUnit's API, engine, and Gradle runner

JUnit Jupiter supplies the JUnit 5 programming model. Gradle must use the JUnit Platform, and the test runtime must include an appropriate engine. A test annotation compiling does not prove tests are being discovered.

For a Kotlin/JVM learning project, use your version catalog or compatible pinned dependencies for Jupiter and the platform launcher. Configure:

// build.gradle.kts in a Kotlin/JVM project
tasks.test {
    useJUnitPlatform()
}
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals

class LabelTest {
    @Test
    fun trimsWhitespace() {
        assertEquals("Kotlin", " Kotlin ".trim())
    }
}

Do not mix JUnit 4 annotations and Jupiter assumptions accidentally. Android instrumented-test integration is a separate setup, so this Gradle example is intentionally for a JVM project.

Exercise

Run the suite from Gradle and confirm the report contains this test. Make the assertion wrong and verify a failing exit status. Restore it afterward.

Check: a green build with zero discovered tests is not evidence that your behavior was verified.

Reference: JUnit with Kotlin

YOUR LEARNING JOURNEY

0 of 110 available lessons completed

Progress saved in this browser. No account needed.
JUnit 5 with Kotlin | Kotlin Core Programming | Android Engineers