环境

springboot2
spring-boot-test 2.5.15

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

报错提示

Test ignored.

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

报错解答

这个错误提示表明测试类没有找到Spring Boot应用程序类,因此无法自动配置Spring上下文。您需要指定要加载的配置类或者使用一个可以自动识别Spring Boot应用程序类的注解。

解决方法

在的测试类上添加注解 @SpringBootTest(classes = YourSpringBootApplication.class) 来指定要加载的Spring Boot应用程序类,其中 YourSpringBootApplication 是应用程序启动类。或者,可以在 @SpringBootTest 注解中使用 properties 属性来指定应用程序的配置文件。

@SpringBootTest(classes = YourSpringBootApplication.class, properties = "spring.config.location=classpath:/application-test.yml")
这里 classpath:/application-test.yml 指定要使用的测试配置文件,以便正确加载应用程序上下文。

我的代码
在这里插入图片描述

结果


成功测试

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐