低版本springboot 單元測試無法注入 Bean 的問題
版本
Springboot
: 1.5.13 ReleaseSpring-cloud
:Edgware.SR3
Junit 版本不兼容導致的問題特徵
- 單元測試無法注入bean
- 測試用例 運行時,沒有出現
Springboot
啓動的界面(即打印的日誌) IDEA
提示@Test
需要單獨 引入 Junit 的Maven 依賴
常見問題發生場景
Springboot
版本更改多次,最後採取較低版本,最後爲了不報錯,單獨引入不適配的、較高版本的Junit。由於 Junit4 與 Junit5 的調用是不同的,因此會導致 無法啓動 SpringBoot
,自然也就無法 注入想要的Bean
解決方法
- 不需要(刪除)單獨引入 Junit 的依賴,一般
springboot
已經自帶了 Junit 的依賴,如果單獨引入可能會覆蓋自帶的Junit
附
低版本Junit單元測試寫法
@SpringBootTest
@RunWith(SpringRunner.class)
@EnableFeignClients(basePackages = "com.lingo.commonremote.client")
public class ThirdServiceApplicationTests {
@Resource
private CommonApiClient commonApiClient;
@Test
public void contextLoads() {
}
@Test
public void $1TestList(){
QueryListParam queryListParamApi = new QueryListParam();
MyQueryWrapper<ErpRelativeDTO> queryWrapper = MyQueryWrapper.getInstance(ErpRelativeDTO.class);
queryWrapper.eq(ErpRelativeDTO::getFisDelete,0);
queryListParamApi.setMyQueryWrapper(queryWrapper);
queryListParamApi.setFormId("erpRelative");
MyResponse<List> list = commonApiClient.list(queryListParamApi);
System.out.println(list.getRetEntity());
}