异常之Unable to find a @SpringBootConfiguration

布满荆棘的人生 2022-03-31 11:54 627阅读 0赞

问题如下

用springboot搭建的项目,测试组件用testng,出现如下问题,找了一会没发现问题根源…问题如下:

  1. Caused by: java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
  2. at org.springframework.util.Assert.state(Assert.java:73)
  3. at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:243)
  4. at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:155)
  5. at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:395)
  6. at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:312)
  7. at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:265)
  8. at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
  9. at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:99)
  10. at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:139)
  11. at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)
  12. at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.<init>(AbstractTestNGSpringContextTests.java:112)
  13. at com.example.redissson.BaseTestNg.<init>(BaseTestNg.java:12)
  14. at com.example.redissson.testCon.<init>(testCon.java:12)
  15. ... 27 more

其他 BaseTestNg.java 文件如下:

  1. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  2. public class BaseTestNg extends AbstractTestNGSpringContextTests {
  3. }

testCon.java 文件如下:

  1. public class testCon extends BaseTestNg{
  2. @Autowired
  3. TestController testController;
  4. @Test
  5. public void tests(){
  6. String test = testController.test();
  7. System.out.println(""+test);
  8. }
  9. }

经过一番查找,发现是test下的目录与main下目录名称不一致导致
在这里插入图片描述
修改为一致解决问题…

发表评论

表情:
评论列表 (有 0 条评论,627人围观)

还没有评论,来说两句吧...

相关阅读