【问题标题】:Hoverfly simulationMode conflicts with Spring Cloud Config Server in unit testHoverfly 模拟模式与单元测试中的 Spring Cloud Config Server 冲突
【发布时间】: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


    【解决方案1】:

    Hoverfly 的开发者 Tommy 在他们的 email list 中回复了我

    这是一个已知问题:https://github.com/SpectoLabs/hoverfly-java/issues/19


    更新

    Tommy Situ 已修复此问题,代码修复将在 v0.4.3

    中发布

    【讨论】:

      猜你喜欢
      • 2018-02-20
      • 2015-01-23
      • 2017-11-12
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 2018-02-07
      相关资源
      最近更新 更多