You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring单元测试报错:无法加载CacheAwareContextLoaderDelegate的解决

解决Spring单元测试中Could not load CacheAwareContextLoaderDelegate错误

问题根源

你遇到的错误是版本兼容性冲突导致的:

  • Spring Boot 2.7.7的父依赖管理的是Spring 5.3.x系列组件,而你手动指定了spring-test版本为6.0.3,这属于Spring 6系列。
  • Spring 6要求Java版本至少为17,但你的项目用的是Java 11,版本不匹配直接引发了类加载失败。

解决方案

直接移除pom.xml中spring-test依赖的版本指定,让它继承Spring Boot父项目的版本管理配置即可。

示例修改前的依赖:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>6.0.3</version>
    <scope>test</scope>
</dependency>

修改后:

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

原理说明

Spring Boot的父依赖已经为所有Spring生态组件做了版本适配,确保各组件版本互相兼容且匹配当前Spring Boot版本。移除手动指定的版本后,Maven会自动拉取与Spring Boot 2.7.7匹配的spring-test(Spring 5.3.x系列),这个版本和Java 11完全兼容,因此测试可以正常运行。

内容的提问来源于stack exchange,提问作者Fazal Haroon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 10:01:43