无论何时添加spring-kafka-test
作为依赖项,我都无法运行测试。
测试开始失败,原因如下:
org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 1.
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:64)
...
Caused by: java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestExecutionListener
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
...
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:97)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:62)
... 18 more
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.TestExecutionListener
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 50 more
我的gradle是这样的:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.tasks.bundling.BootJar
plugins {
id("org.springframework.boot") version "3.0.0"
id("io.spring.dependency-management") version "1.1.0"
kotlin("jvm") version "1.7.22"
kotlin("plugin.spring") version "1.7.22"
`maven-publish`
`java-test-fixtures`
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
api("org.jetbrains.kotlin:kotlin-reflect")
api("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
api("com.auth0:java-jwt:4.2.1")
api("com.fasterxml.jackson.module:jackson-module-kotlin")
api("io.github.microutils:kotlin-logging-jvm:3.0.4")
api("org.springframework.boot:spring-boot-starter")
api("org.springframework.boot:spring-boot-starter-actuator")
api("org.springframework.boot:spring-boot-starter-data-mongodb")
api("org.springframework.boot:spring-boot-starter-validation")
api("org.springframework.boot:spring-boot-starter-security")
api("org.springframework.boot:spring-boot-starter-web")
api("org.springframework.kafka:spring-kafka")
testFixturesApi("org.springframework.boot:spring-boot-starter-test")
testFixturesApi("org.springframework.restdocs:spring-restdocs-mockmvc")
testFixturesApi("org.springframework.security:spring-security-test")
testFixturesApi("org.springframework.kafka:spring-kafka-test")
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()
artifactId = project.name
version = project.version.toString()
from(components["java"])
}
}
}
tasks.getByName<BootJar>("bootJar") {
enabled = false
}
tasks.getByName<Jar>("jar") {
enabled = true
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
我的配置中缺少什么?这是什么原因,因为我知道一切都应该工作良好。
这是spring-kafka-test
版本3.0.0
中的一个错误,将在3.0.1
中修复。
现在,您可以通过在gradle文件中手动添加对org.junit.platform:junit-platform-launcher
的测试依赖来缓解这个问题。
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
testImplementation("org.junit.platform:junit-platform-launcher:1.9.1")
testImplementation("org.springframework.kafka:spring-kafka-test:3.0.0")
}
见:https://github.com/spring-projects/spring-kafka/issues/2490