如何在Kotlin DSL中使用Gradle中的本机JUnit 5支持? [英] How do I use the native JUnit 5 support in Gradle with the Kotlin DSL?

查看:98
本文介绍了如何在Kotlin DSL中使用Gradle中的本机JUnit 5支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Gradle Kotlin DSL中使用内置的JUnit 5,因为在构建过程中会收到以下警告:

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

那个链接告诉我放

test {
    useJUnitPlatform()
}

我的build.gradle中的

,但是build.gradle.kts的语法是什么?

我当前的构建文件是

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()
}

(以下是一些非议,因为此问题主要包含代码"). 我试图找到有关如何在Kotlin DSL中自定义任务的文档,但找不到任何文档.在普通的Groovy中,您可以只写任务的名称,然后在块中进行更改,但是Kotlin DSL无法将任务识别为无法解析的引用.

此问题也与此相关,但要求创建 new 任务,而不是自定义现有任务:这是普通Gradle的解决方案.

解决方案

[2019年4月编辑]由于 Pedro已找到 ,在我问了这个问题三个月后,Gradle实际上为Kotlin DSL创建了一个用户指南,可以在 https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/

答案:

您要求的语法是

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

我从示例文件(来自Kotlin DSL GitHub),或者您可以使用

tasks.withType<Test> {
    useJUnitPlatform()
}

此官方用户指南中使用的

写下答案后的几个月(感谢 Pedro的答案指出了这一点).

但是在任何情况下,您实际上仍在使用buildscript块(本身已过时了),请改用新的plugins DSL(

(由于Gradle Kotlin DSL几乎没有任何文档,只有少数(未记录)示例文件 ,我在这里记录了一些常见的示例.)

(完成 GitHub 上的示例项目,自我推广...)

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.

解决方案

[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 {
    // Use the built-in JUnit support of Gradle.
    "test"(Test::class) {
        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...)

这篇关于如何在Kotlin DSL中使用Gradle中的本机JUnit 5支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆