SpringBoot测试失败并报错: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration

该测试类在测试包test下的包名和类路径java下的包名不一致导致的,修改包名一致即可
SpringBoot测试失败并报错: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration_第1张图片
由于包名自动生成的缘故导致这两个包名不一致,引发以下报错

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

解决:修改包名一致,如图
SpringBoot测试失败并报错: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration_第2张图片
问题解决

2 如果不想修改包名,那么需要在注解上加上@SpringBootTest(classes = Application.class),来告诉springboot这是一个独立的测试类
SpringBoot测试失败并报错: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration_第3张图片

或者使用

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JPAConfig.class})

你可能感兴趣的:(springboot,error,java,spring,boot,spring,bug)