运行KafkaEmbedded进行单元测试时的java.lang.NoClassDefFoundError

时间:2018-06-06 22:50:54

标签: spring spring-boot spring-kafka

我有一个运行Spring Boot 2.0.2的Spring-Kafka项目。在单元测试中,我正在尝试启动KafkaEmbedded服务器。我收到了这个错误。

java.lang.NoClassDefFoundError: org/apache/kafka/common/security/auth/SecurityProtocol

at kafka.utils.TestUtils$.createBrokerConfig(TestUtils.scala:222)
at kafka.utils.TestUtils.createBrokerConfig(TestUtils.scala)
at org.springframework.kafka.test.rule.KafkaEmbedded.createBrokerProperties(KafkaEmbedded.java:278)
at org.springframework.kafka.test.rule.KafkaEmbedded.before(KafkaEmbedded.java:224)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.common.security.auth.SecurityProtocol
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 13 more

我知道这是因为依赖项不匹配。但我已经添加了最新版本spring-kafka-test&卡夫卡的客户端。我知道这个班级" SecurityProtocol"是在Kafka客户端jar,但添加依赖(旧版本和新版本)仍然无法解决问题。

以下是pom文件中的依赖项。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka-test</artifactId>
        <version>2.1.6.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>1.0.0</version>
        <scope>test</scope>
    </dependency>

以下是使用EmbeddedKafka的基本单元测试。

@RunWith(SpringRunner.class)
@SpringBootTest
public class KafkaDemoApplicationTests {

    @ClassRule
    public static KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, true,
            Topology.DEMO_TOPIC);

    @Test
    public void contextLoads() {
    }

}

3 个答案:

答案 0 :(得分:2)

你在类路径上有错误的kafka(scala)jar。

  

java.lang.NoClassDefFoundError:org / apache / kafka / common / security / auth / SecurityProtocol

SecurityProtocol已在kafka-clients 1.0.0中从org.apache.kafka.common.protocol移至org.apache.kafka.common.security.auth

所以看起来你(不知何故)在CP上得到了旧的kafka_2.11 jar; spring-kafka-test应引入正确的版本。也许你在你的pom中覆盖了你没有表现出来的东西?

答案 1 :(得分:1)

解决方案是避免在Spring Boot项目的pom.xml文件中为所有与Kafka相关的依赖项指定版本。像这样 -

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka-test</artifactId>
        <scope>test</scope>
    </dependency>

答案 2 :(得分:1)

如果您使用的是kafka-client(不是spring kafka),请确保pom中使用的kafka-client版本与spring-kafka测试依赖项中使用的版本相同。 在spring-kafka-test Maven存储库[https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka-test]中,它列出了所有依赖项,包括它使用的kafka-client。 因此,要么更新您的kafka客户端版本,要么更新spring-kafka-test版本 确保客户相同。