最近單元測試時碰到需要mock feginClient的接口
因為項目中存在fegin的hystrix,在mock的時候會裝在其中一個bean , cloud中的fegin是接口而hystrix又是接口的實現(xiàn),所以在測試的時候會報錯:
java.lang.IllegalStateException: Unable to register mock bean?..* expected a single matching bean to replace but found [..client,..clientHystrix]
其實測試啟動@mockbean會使用自己裝載的bean替換項目中的 找到了兩個類似實體會失敗。
解決:
在hystrix中增加@Profile("!test")對測試時忽略裝載的狀態(tài)
在test Class中加入@ActiveProfiles("test")? 保證hystrix在測試時不會被檢測裝載 也就不會和相應(yīng)的Fegin發(fā)生沖突