我在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,我可以设置一些配置并排除配置服务器的网址吗?