具體步驟參考該文:https://blog.csdn.net/dongzi_yu/article/details/106844732
但是照做之后失敗,后來發現是因為注解@SpringBootTest后面沒有指明SpringBoot啟動類是哪個
完整的寫法應該是:
@RunWith(SpringRunner.class) //作用:作為啟動器,讓junit跟spring環境整合
@SpringBootTest(classes={APP.class}) // 作用:1.標記當前類為測試類 2.加載spring-boot啟動類,啟動spring-boot
在未指定啟動類的情況下會報:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
參考: