SpringBoot JUnit4测试报错:未找到@ContextConfiguration或@ContextHierarchy
SpringBoot测试上下文加载失败问题解决
问题场景
使用SpringBoot 3.0.4搭建新项目,编写MongoDB文档转Java对象的测试时,运行测试出现上下文加载相关错误,无法正常启动Spring容器。
项目配置文件(build.gradle)
plugins { id 'java' id 'org.springframework.boot' version '3.0.4' id 'io.spring.dependency-management' version '1.1.0' } group = 'com.kriegzeug' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.2' } tasks.named('test') { useJUnitPlatform() }
测试代码
@RunWith(SpringRunner.class) @SpringBootTest public class DataModelTests { @Autowired CardRepository cardRepository; @Test public void SoloWithNoAttacks() { // GIVEN String cardName = "Mortitheurge Willbreaker"; // WHEN Card card = cardRepository.findCardByName(cardName); // THEN Assertions.assertEquals("Mortitheurge Willbreaker", card.getName()); Assertions.assertEquals(3, card.getEdition()); Assertions.assertEquals("2021 v1", card.getRevision()); Assertions.assertEquals(List.of("Skorne", "Solo"), card.getKeywords()); } }
错误日志
14:14:10.538 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [DataModelTests]: using SpringBootContextLoader 14:14:10.542 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.kriegzeug.college.DataModelTests]: no resource found for suffixes {-context.xml, Context.groovy}. 14:14:10.542 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.kriegzeug.college.DataModelTests]: DataModelTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 14:14:10.553 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using ContextCustomizers for test class [DataModelTests]: [DisableObservabilityContextCustomizer, PropertyMappingContextCustomizer, Customizer, ExcludeFilterContextCustomizer, DuplicateJsonObjectContextCustomizer, MockitoContextCustomizer, TestRestTemplateContextCustomizer]
解决步骤
1. 修复SpringBoot启动类扫描范围
@SpringBootTest默认会扫描测试类所在包及其父包下的@SpringBootApplication启动类。如果启动类(如XXXApplication.java)不在测试类的父包或同包范围内,就会找不到配置。
解决方式:
- 把启动类移到
com.kriegzeug根包(符合SpringBoot最佳实践); - 或者在
@SpringBootTest注解里显式指定启动类:@SpringBootTest(classes = YourApplication.class)
2. 移除JUnit Vintage依赖
SpringBoot 3.x基于JUnit 5,junit-vintage-engine是用于兼容JUnit 4的依赖,和当前测试使用的JUnit 5 API存在冲突,会干扰测试上下文加载。
修改build.gradle的dependencies,删除以下行:
testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.2'
spring-boot-starter-test已包含JUnit 5的所有必要依赖,无需额外添加。
3. 简化测试注解(可选)
SpringBoot 2.2及以上版本中,@SpringBootTest已默认集成@RunWith(SpringRunner.class)的功能,可直接去掉该注解简化测试类:
@SpringBootTest public class DataModelTests { // ... 测试代码不变 }
内容的提问来源于stack exchange,提问作者OnlyDean
相关产品推荐
相关产品推荐

