SpringBoot运行单元时测试报错:java.lang.IllegalStateException Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or…

错误描述

SpringBoot运行单元测试时报错:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test

我的软件环境

JDK 11.0

SpringBoot 2.7

Maven 3.8.7

出错原因

src/test/java目录下的包结构和src/main/java目录下的包结构应该一样,否则就会报上述错误。

我的src/main/java目录下的包是com.comp.example,但是src/test/java目录下的包是com.sample.example,两者不一样,因此报上述错误。

解决方法

把src/test/java目录下的包结构和src/main/java目录下的包结构改成一样即可。

参考

https://stackoverflow.com/questions/47487609/unable-to-find-a-springbootconfiguration-you-need-to-use-contextconfiguration

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注