1. 程式人生 > >異常之Unable to find a @SpringBootConfiguration

異常之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下目錄名稱不一致導致
在這裡插入圖片描述
修改為一致解決問題…