异常之Unable to find a @SpringBootConfiguration

问题如下

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

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

其他 BaseTestNg.java 文件如下:

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

testCon.java 文件如下:

public class testCon extends BaseTestNg{
    @Autowired
    TestController testController;

    @Test
    public void tests(){
        String test = testController.test();
        System.out.println(""+test);
    }
}

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

猜你喜欢

转载自blog.csdn.net/u010520146/article/details/85684039