ClassNotFoundException:scala.collection.convert.AsJavaExtensions 使用来自 spring-kafka-test 的 @EmbeddedKafka 时

ClassNotFoundException: scala.collection.convert.AsJavaExtensions when using @EmbeddedKafka from spring-kafka-test

当尝试使用 spring-kafka-test 中的 @EmbeddedKafka 时,我在 运行 测试时得到 ClassNotFoundException: scala.collection.convert.AsJavaExtensions(完整堆栈跟踪 here)。

我尝试了不同的 Kafka 版本(org.apache.kafka:kafka_2.11:2.4.1 而不是 org.apache.kafka:kafka_2.12:2.8.0)以及 org.scala-lang:scala-library 的不同版本(2.13.62.12.102.11.12, 2.10.6).

我需要做什么才能解决这个问题?

下面是重现问题的最小示例:

src/main/kotlin/com/acme/Application.kt

package com.acme

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}

src/test/kotlin/com/acme/integration/TestFullStack.kt

package com.acme.integration

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.kafka.test.context.EmbeddedKafka
import org.springframework.test.context.junit.jupiter.SpringExtension

@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EmbeddedKafka(topics = ["testtopic"], controlledShutdown = true)
class TestFullStack {
    @Test
    fun `test init`() {
    }
}

build.gradle.kts

plugins {
    id("org.springframework.boot") version "2.5.4"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.5.21"
    kotlin("plugin.spring") version "1.5.21"
}

group = "com.acme"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation(group = "org.apache.kafka", name = "kafka_2.12", version = "2.8.0")
    implementation(group = "org.scala-lang", name = "scala-library", version = "2.12.10")
    implementation(group = "org.springframework.kafka", name = "spring-kafka", version = "2.7.6")
    implementation(group = "org.springframework.boot", name = "spring-boot-starter-web")
    implementation(group = "org.jetbrains.kotlin", name = "kotlin-reflect")

    testImplementation(group = "org.springframework.boot", name = "spring-boot-starter-test")
    testImplementation(group = "org.springframework.kafka", name = "spring-kafka-test", version = "2.7.6")
    testImplementation(group = "org.testcontainers", name = "kafka", version = "1.16.0")
    testImplementation(group = "org.jetbrains.kotlin", name = "kotlin-test-junit")
}

tasks {
    withType<Test> {
        testLogging.exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
        useJUnitPlatform()
    }
}

settings.gradle.kts

rootProject.name = "acmetest"

Dockerfile

FROM openjdk:11-jdk-slim
WORKDIR /home/test
ADD . /home/test
RUN ./gradlew test

通过使用正确的依赖项使其工作。

dependencies {
    implementation(group = "org.springframework.boot", name = "spring-boot-starter-web")
    implementation(group = "org.apache.kafka", name = "kafka_2.13")
    implementation(group = "org.apache.kafka", name = "kafka-clients")

    testImplementation(group = "org.springframework.boot", name = "spring-boot-starter-test")
    testImplementation(group = "org.jetbrains.kotlin", name = "kotlin-test-junit")
    testImplementation(group = "org.springframework.kafka", name = "spring-kafka-test")
}