【问题标题】:ClassNotFoundException: scala.collection.convert.AsJavaExtensions when using @EmbeddedKafka from spring-kafka-testClassNotFoundException:scala.collection.convert.AsJavaExtensions 使用来自 spring-kafka-test 的@EmbeddedKafka 时
【发布时间】:2021-11-09 17:58:35
【问题描述】:

当尝试从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.122.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

【问题讨论】:

    标签: spring-boot kotlin gradle spring-kafka spring-kafka-test


    【解决方案1】:

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

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

    【讨论】:

    • 参考手册中的一个附录说明所有的jar必须是相同的版本:docs.spring.io/spring-kafka/docs/current/reference/html/…
    • @GaryRussell 所以我可以让 Spring Boot 的依赖管理为我选择版本。我试过了,效果很好。谢谢! (我已经为未来的读者调整了答案。)
    • 正确;如果要覆盖 Boot 的默认版本,只需指定版本(针对所有与 kafka 相关的 jar),然后,它们必须都是相同的版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2020-10-12
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多