【发布时间】:2017-03-09 07:40:33
【问题描述】:
我在 Spring Boot 项目的单元测试中使用 hoverfly。
背景
spring boot 项目将从 spring 云配置服务器获取其配置(连接超时等)。 为了测试我的超时配置是否有效,我编写了一个单元测试,并期望 hoverfly 可以长时间延迟返回,然后我自定义的 restTemplate 可以抛出超时错误而不是等待。 单元测试看起来像这样:
@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部分提到的,当我的spring boot项目启动时,它会从spring cloud config server中获取configs,但是Hoverfly捕获了该请求并尝试找到相应的记录,当然不能,因为我只为我的单元测试定义了记录(例如 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,我可以设置一些配置并排除配置服务器的url吗?
【问题讨论】:
标签: java unit-testing spring-boot