Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the native JUnit 5 support in Gradle with the Kotlin DSL?

I want to use the built-in JUnit 5 with the Gradle Kotlin DSL, because during build I get this warning:

WARNING: The junit-platform-gradle-plugin is deprecated and will be discontinued in JUnit Platform 1.3. Please use Gradle's native support for running tests on the JUnit Platform (requires Gradle 4.6 or higher): https://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle 

That links tells me to put

test {     useJUnitPlatform() } 

in my build.gradle, but what is the syntax for build.gradle.kts?

My current build file is

import org.gradle.api.plugins.ExtensionAware  import org.junit.platform.gradle.plugin.FiltersExtension import org.junit.platform.gradle.plugin.EnginesExtension import org.junit.platform.gradle.plugin.JUnitPlatformExtension  group = "com.example" version = "0.0"  // JUnit 5 buildscript {     repositories {         mavenCentral()         jcenter()     }     dependencies {         classpath("org.junit.platform:junit-platform-gradle-plugin:1.2.0")     } }  apply {     plugin("org.junit.platform.gradle.plugin") }  // Kotlin configuration. plugins {      val kotlinVersion = "1.2.41"      application     kotlin("jvm") version kotlinVersion     java // Required by at least JUnit.      // Plugin which checks for dependency updates with help/dependencyUpdates task.     id("com.github.ben-manes.versions") version "0.17.0"      // Plugin which can update Gradle dependencies, use help/useLatestVersions     id("se.patrikerdes.use-latest-versions") version "0.2.1"  }  application {     mainClassName = "com.example.HelloWorld" }  dependencies {     compile(kotlin("stdlib"))     // To "prevent strange errors".     compile(kotlin("reflect"))     // Kotlin reflection.     compile(kotlin("test"))     compile(kotlin("test-junit"))      // JUnit 5     testImplementation("org.junit.jupiter:junit-jupiter-api:5.2.0")     testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")     testRuntime("org.junit.platform:junit-platform-console:1.2.0")      // Kotlintest     testCompile("io.kotlintest:kotlintest-core:3.1.0-RC2")     testCompile("io.kotlintest:kotlintest-assertions:3.1.0-RC2")     testCompile("io.kotlintest:kotlintest-runner-junit5:3.1.0-RC2")  }  repositories {     mavenCentral()     mavenLocal()     jcenter() } 

(The following is some blabla because this question 'contains mostly code'). I tried to find documentation on how to customize tasks in the Kotlin DSL, but I couldn't find any. In normal Groovy you can just write the name of the task and then change things in the block, but the Kotlin DSL doesn't recognise the task as such, unresolved reference.

Also, this question is related but asks for creating of new tasks, instead of customize existing tasks: How do I overwrite a task in gradle kotlin-dsl

Here is a solution for normal Gradle.

like image 873
PHPirate Avatar asked May 02 '18 06:05

PHPirate


People also ask

What is the Kotlin Gradle DSL?

Gradle's Kotlin DSL provides an alternative syntax to the traditional Groovy DSL with an enhanced editing experience in supported IDEs, with superior content assist, refactoring, documentation, and more.

What is DSL in Gradle?

Simply, it stands for 'Domain Specific Language'. IMO, in gradle context, DSL gives you a gradle specific way to form your build scripts. More precisely, it's a plugin-based build system that defines a way of setting up your build script using (mainly) building blocks defined in various plugins.

How add Kotlin plugin to Gradle?

To add Kotlin to your project, do the following: Click File > New, and choose one of the various Android templates, such as a new blank Fragment, as shown in figure 1. If you don't see the list of templates in this menu, first open the Project window, and select your app module.

Does Gradle work with Kotlin?

Gradle is a build system that is very commonly used in the Java, Android, and other ecosystems. It is the default choice for Kotlin/Native and Multiplatform when it comes to build systems.


1 Answers

[Edit april 2019] As Pedro has found, three months after I asked this question Gradle actually created a user guide for the Kotlin DSL which can be visited at https://docs.gradle.org/current/userguide/kotlin_dsl.html

They also added a migration guide from Groovy to Kotlin at https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/

Answer:

The syntax you ask for is

tasks.test {     // Use the built-in JUnit support of Gradle.     useJUnitPlatform() } 

which I figured out from this example file from the Kotlin DSL GitHub, or you can use

tasks.withType<Test> {     useJUnitPlatform() } 

which is used in the this official userguide which was created a couple of months after this answer was written (thanks to Pedro's answer for noting this).

But in any case you actually are still using the buildscript block, which is a bit deprecated itself, use the new plugins DSL instead (docs). New build.gradle.kts becomes

group = "com.example" version = "0.0"  plugins {      val kotlinVersion = "1.2.41"      application     kotlin("jvm") version kotlinVersion     java // Required by at least JUnit.      // Plugin which checks for dependency updates with help/dependencyUpdates task.     id("com.github.ben-manes.versions") version "0.17.0"      // Plugin which can update Gradle dependencies, use help/useLatestVersions     id("se.patrikerdes.use-latest-versions") version "0.2.1" }  application {     mainClassName = "com.example.HelloWorld" }  dependencies {     compile(kotlin("stdlib"))     // To "prevent strange errors".     compile(kotlin("reflect"))     // Kotlin reflection.     compile(kotlin("test"))     compile(kotlin("test-junit"))      // JUnit 5     testImplementation("org.junit.jupiter:junit-jupiter-api:5.2.0")     testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")     testRuntime("org.junit.platform:junit-platform-console:1.2.0")      // Kotlintest     testCompile("io.kotlintest:kotlintest-core:3.1.0-RC2")     testCompile("io.kotlintest:kotlintest-assertions:3.1.0-RC2")     testCompile("io.kotlintest:kotlintest-runner-junit5:3.1.0-RC2")  }  repositories {     mavenCentral()     mavenLocal()     jcenter() }  tasks {     // Use the native JUnit support of Gradle.     "test"(Test::class) {         useJUnitPlatform()     } } 

(Since the Gradle Kotlin DSL has almost no documentation at all except a few (undocumented) example files on GitHub, I'm documenting a few common examples here.)

(Complete example project at GitHub, self-promotion...)

like image 82
PHPirate Avatar answered Sep 26 '22 00:09

PHPirate