1. 程式人生 > >spring boot 單元測試錯誤

spring boot 單元測試錯誤

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

之前一直都不怎麼習慣使用單元測試,最近在學習spring boot 試了一下,記錄一下遇到的問題

@RunWith(SpringRunner.class)
@SpringBootTest
public class PesionTest {

    @Autowired
    ApplicationContext app;

    @Test
public void getService() { System.out.println(app.containsBean("helloService")); } }

遇到了java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test 問題原因是單元測試類需要和主程式類在相同包下
這裡寫圖片描述