当使用 Spring Boot 构建微服务时,可以非常轻松地编写大量且非常易读的集成测试并模拟远程服务请求MockRestServiceServer
.
有没有办法使用类似的方法来执行额外的集成测试ZuulProxy
?我想要实现的是能够模拟远程服务器ZuulProxy
将转发并验证我的所有ZuulFitler
s 的表现符合预期。然而,ZuulProxy
正在使用RestClient
来自 Netflix(似乎已弃用?),它自然不使用RestTemplate
可以通过以下方式重新配置MockRestServiceServer
我目前找不到模拟远程服务对代理请求的响应的好方法。
我有一个微服务,负责处理 API 会话密钥的创建,然后其行为类似于 API 网关。转发是通过 Zuul Proxy 完成到底层公开服务的,Zuul Filters 将检测会话密钥是否有效。因此,集成测试将创建一个有效的会话,然后转发到一个假端点,例如“集成/测试”。
可以通过设置配置属性来指定“集成/测试”是新端点@WebIntegrationTest
,我可以成功模拟所有正在处理的服务RestTemplate
但没有 Zuul 转发。
实现对转发目标服务的模拟的最佳方法是什么?
查看WireMock http://wiremock.org/docs/junit-rule/。我一直在使用它对我的 Spring Cloud Zuul 项目进行集成级别测试。
import static com.github.tomakehurst.wiremock.client.WireMock.*;
public class TestClass {
@Rule
public WireMockRule serviceA = new WireMockRule(WireMockConfiguration.options().dynamicPort());
@Before
public void before() {
serviceA.stubFor(get(urlPathEqualTo("/test-path/test")).willReturn(aResponse()
.withHeader("Content-Type", "application/json").withStatus(200).withBody("serviceA:test-path")));
}
@Test
public void testRoute() {
ResponseEntity<String> responseEntity = this.restTemplate.getForEntity("/test-path/test", String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
serviceA.verify(1, getRequestedFor(urlPathEqualTo("/test-path/test")));
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)