我在spring引導項目的單元測試中使用hoverfly。Hoverfly simulationMode與單元測試中的Spring Cloud Config Server相沖突
背景
彈簧啓動項目將抓住從春天的雲服務器配置其配置(連接超時等)。 爲了測試我的超時配置是否工作,我編寫了一個單元測試,並期望hoverfly可以長時間返回,那麼我的自定義restTemplate可能會拋出超時錯誤而不是等待。 單元測試看起來lilke這樣的:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestApplication.class)
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class CustomRestTemplateTest {
@Autowired
private RestTemplate customRestTemplate;
@ClassRule
public static HoverflyRule hoverflyRule = HoverflyRule.inSimulationMode(SimulationSource.dsl(
service("www.test.com")
.get("/")
.willReturn(success(HttpBodyConverter.json("{}")).withDelay(10, TimeUnit.SECONDS))
));
@Test
public void connectionTimeoutTest() {
customRestTemplate.getForObject("www.test.com", Object.class);
}
}
問題
正如我在部分The background
,我的春天啓動項目啓動時,它會抓住從春天的雲配置服務器CONFIGS,但食蚜蠅捕獲的請求中提到並試圖找到相應的記錄,當然不能,因爲我只定義的記錄我的單元測試(如www.test.com),所以它拋出錯誤:
{"destination":"172.16.2.84:8888","error":"No match found","key":"a7ac72c9bcc3dc2b76bf0877d98f9e3a","level":"warning","method":"GET","msg":"Failed to find matching request template from template store","path":"************","query":"","time":"2017-03-08T20:55:28+08:00"}
我怎麼能修復這個?我想使用hoverfly,我可以設置一些配置並排除配置服務器的網址嗎?