【发布时间】:2020-01-16 14:39:56
【问题描述】:
我的 Gradle 项目使用 KotlinDSL 和 JUnit5 / Jupiter(以及 Kotlin 作为编程语言),Gradle 似乎没有正确地接受我的任何测试。当我在命令行上运行 Gradle 测试时,它会告诉我
SUCCESS: Executed 0 tests in 1s
无论我尝试什么。但是,在我的 IDE 中,我可以轻松地运行项目中的每个测试类,而不会出现任何问题。
我的构建文件 build.gradle.kts 如下所示:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.1.8.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
id("com.adarshr.test-logger") version "1.7.0"
kotlin("jvm") version "1.2.71"
kotlin("plugin.spring") version "1.2.71"
kotlin("plugin.jpa") version "1.2.71"
}
group = "myprojectgroup"
version = "1.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val junitVersion="5.5.2"
val mockitoVersion="2.28.2"
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.flywaydb:flyway-core")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
testCompile ("org.mockito:mockito-core:$mockitoVersion")
testCompile("org.mockito:mockito-junit-jupiter:$mockitoVersion")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
tasks.test {
useJUnitPlatform {
includeEngines("junit-jupiter")
}
}
测试类与预期一样位于 src/test/kotlin 下,测试方法使用正确的 org.junit.jupiter.api.Test 注释进行注释。有什么想法我可以尝试吗?它真的让我发疯了。
非常感谢各位!
康斯坦丁
【问题讨论】:
-
您想将您的项目与 gradle 示例项目进行比较:github.com/gradle/gradle/tree/master/subprojects/docs/src/…
-
感谢您的提示!但是,您提到的项目使用 Java 而不是 Kotlin 作为编程语言,使用 JUnit 4.x 而不是 5.x,所以对我来说,这似乎对我没有帮助。这个堆栈的常见示例也适用于我。
-
有很多不同的语言可用。这里是顶部链接:github.com/gradle/gradle/tree/master/subprojects/docs/src/…
标签: java gradle kotlin junit5 gradle-kotlin-dsl