[已解決]低版本springboot Junit單元測試無法注入 Bean 的問題

低版本springboot 單元測試無法注入 Bean 的問題

版本

  1. Springboot: 1.5.13 Release
  2. Spring-cloud :Edgware.SR3

Junit 版本不兼容導致的問題特徵

  1. 單元測試無法注入bean
  2. 測試用例 運行時,沒有出現 Springboot 啓動的界面(即打印的日誌)
  3. IDEA 提示 @Test 需要單獨 引入 Junit 的Maven 依賴

常見問題發生場景

Springboot版本更改多次,最後採取較低版本,最後爲了不報錯,單獨引入不適配的、較高版本的Junit。由於 Junit4 與 Junit5 的調用是不同的,因此會導致 無法啓動 SpringBoot ,自然也就無法 注入想要的Bean

解決方法

  1. 不需要(刪除)單獨引入 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());
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章