【问题标题】:ClassNotFoundException on org.springframework.test.context.TestContextAnnotationUtils when upgrading Spring Boot to 2.4.2将 Spring Boot 升级到 2.4.2 时,org.springframework.test.context.TestContextAnnotationUtils 上的 ClassNotFoundException
【发布时间】:2021-04-24 16:14:33
【问题描述】:

在我的 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() {
    }

}

有人知道吗?

【问题讨论】:

  • 一般来说,要么你有一个损坏的测试 jar,要么正在混合来自不同版本的 Spring 的 jar。我怀疑是后者,而您正试图通过提供不必要的依赖项或版本标签来超越框架。
  • 这篇文章可能会有所帮助。github.com/spring-projects/spring-boot/wiki/…
  • 显示你的 pom.xml。没有它,所有的帮助都只是猜测。
  • 非常感谢@M.Deinum,您的干预与十年前一样有用!

标签: spring-boot junit5 spring-test


【解决方案1】:

在我的情况下,我的 pom.xml 中的库版本与 Spring Boot 版本冲突,所以我从我的依赖项中删除了该版本,并让 Spring 默认使用配置的版本。

我变了:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test-autoconfigure</artifactId>
    <version>2.6.2</version>
    <scope>test</scope>
</dependency>

到:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      分析依赖关系树和依赖关系列表后,您可能会得到版本过期的信息。尝试从那里排除并包括作为独立的,这有助于我们保持同步。

      Maven 命令:

      mvn dependency:tree
      mvn dependency:list
      

      【讨论】:

        【解决方案4】:

        从 2.4.0 版开始,将不再使用 .RELEASE 标记。详情请查看 Spring 博客new versioning conventions

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            似乎您缺少依赖项。 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>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-10-07
              • 2022-01-18
              • 2021-04-26
              • 2021-12-30
              • 2020-04-29
              • 2021-03-08
              • 1970-01-01
              • 2019-06-08
              相关资源
              最近更新 更多