Kotlin JUnit5

Submitted by Dickens A S on Sun, 12/15/2019 - 05:10

A simple JUnit 5 project with Gradle 6

GitHubhttps://github.com/dickensas/kotlin-gradle-templates/tree/master/junit5

package junit5

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class AppTest {
    @Test fun `test greeting`() {
        val classUnderTest = App()
        assertNotNull(classUnderTest.greeting, "app should have a greeting")
    }
}

Note: a fresh gradle project always generates code for JUnit 4, for JUnit 5 you need to change the dependencies accordingly to jupiter package

 

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.3.61"
    application
}

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    testImplementation("org.junit.jupiter:junit-jupiter-api:5.5.2")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.5.2")
}

application {
    mainClassName = "junit5.AppKt"
}

tasks.withType<Test> {
    useJUnitPlatform()
}

 

Add new comment