将 Spring Boot 升级到 2.4.2 时 org.springframework.test.context.TestContextAnnotationUtils 上的 ClassNotFoundException

问题描述

在我的 POM 中,我将 Spring Boot 的版本从 2.3.5.RELEASE 升级到 2.4.2。结果,mvn clean test 现在失败并出现错误

java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils
Caused by: java.lang.classNotFoundException: org.springframework.test.context.TestContextAnnotationUtils

一个简单的测试中

package ch.ge.ael.enu.mediation;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBoottest;

@SpringBoottest
class MediationApplicationTests {

    @Test
    void contextLoads() {
    }

}

有人知道吗?

解决方法

Marten Deinum 的评论是正确的:依赖在 Spring JAR 的版本之间造成了严重破坏。对于 Camel 3.6.0,Spring Boot 2.3.5.RELEASE 可以,但 Spring Boot 2.4.2 不行。将 Camel 升级到 3.7.1 就成功了。

,

为我解决这个问题的是将我的 spring-test 依赖版本从 5.2.9.RELEASE 升级到 5.3.8

,

似乎您缺少依赖项。 spring-boot-starter-test-2.4.2引用的org.springframework.test.context.TestContextAnnotationUtils类位于

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.3</version>
    </dependency>
,

所描述的问题与不同Maven依赖项之间的冲突有关。我们在尝试使用最新版本的 Spring Boot 时遇到了同样的问题。

在分析依赖树和依赖列表后,您可能会知道哪个版本已过时。尝试从那里排除并包括作为独立的内容,这有助于我们保持同步。

Maven 命令:

mvn dependency:tree
mvn dependency:list
,

从 2.4.0 版本开始,将不再使用 .RELEASE 标签。查看 Spring 博客,了解有关 new versioning conventions 的详细信息: