bigywater.tech
Spring · 158

Unable to find a @SpringBootConfiguration:测试里为什么找不到主类

测试里报 Unable to find a @SpringBootConfiguration 是怎么回事?测试类放在哪个包才找得到?补救办法各有什么副作用?这篇在 Spring Boot 4.0.8 上把包的位置逐个试了一遍。

52 对 8
补救办法 classes = App.class 得到 52 个 Bean 定义,测试类里嵌一个 @Configuration 只得到 8 个、Greeter 是 0 个
父包也找不到
测试类放在主类包的父包里,同样报 Unable to find——只往上找,不往下找
@TestConfiguration 不算
嵌套的 @TestConfiguration 类不会被当成主配置

包的位置

实测 · 本机执行输出
  测试类在 demo.app(和主类同一个包):通过:上下文加载成功,Bean 定义数 52;Greeter Bean:1 个
  测试类在 demo.app.sub(主类包的子包):通过:上下文加载成功,Bean 定义数 52;Greeter Bean:1 个
  测试类在 demo.test(主类包的兄弟包):失败:Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
  测试类在 demo(主类包的父包):失败:Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.
  测试类在 other.pkg(完全不相关的包):失败:Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.

同包与子包能找到,兄弟包、父包、无关包找不到。

补救

实测 · 本机执行输出
  @SpringBootTest(classes = App.class):通过:上下文加载成功,Bean 定义数 52;Greeter Bean:1 个
  @SpringBootTest + @ContextConfiguration(classes = App.class):通过:上下文加载成功,Bean 定义数 52;Greeter Bean:1 个
  测试类里有一个嵌套的 @Configuration 类:通过:上下文加载成功,Bean 定义数 8;Greeter Bean:0 个
  测试类里有一个嵌套的 @TestConfiguration 类:失败:Unable to find a @SpringBootConfiguration by searching packages upwards from the test. You can use @ContextConfiguration, @SpringBootTest(classes=...) or other Spring Test supported mechanisms to explicitly declare the configuration classes to load. Classes annotated with @TestConfiguration are not considered.

classes = App.class 或 @ContextConfiguration;嵌套 @Configuration 只得到一个几乎空的上下文。

多个主类

实测 · 本机执行输出
  demo.app2 里有 App2 和 App3 两个主类,测试类在 demo.app2:失败:Found multiple @SpringBootConfiguration annotated classes [Generic bean: class=demo.app2.App2; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; fallback=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null; defined in fi

同一个包里有两个主类时报 Found multiple,要用 classes 指定。

分步演示

测试
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / OpenJDK 21.0.12(Homebrew) / Spring Boot 4.0.8(Spring Framework 7.0.9、Tomcat 11.0.24) / Spring Cloud 2025.1.3;用 javac 编译、java 直接运行,依赖由 Maven 从 Maven Central 解析;数字每次运行会有小幅抖动(Spring Boot 4.0.8 的 spring-boot-starter-test,JUnit Jupiter;每个测试类单独起一个 JVM,用 JUnit Platform Console Launcher 运行;主类 demo.app.App 与一个 @Component 的 Greeter)
    • 没有验证:@WebMvcTest、@DataJpaTest 等切片测试注解的同类报错;@SpringBootTest 与 @ContextHierarchy 的组合;Maven 与 Gradle 里测试类目录与主代码目录分离时的差别;测试类放在默认包(没有包名)里的情况;Kotlin 与 Groovy;嵌套配置里再加 @ComponentScan 的写法
    京ICP备2026067502号-1京公网安备11010502063487号