Spring Boot +云| Zuul代理|整合测试 [英] Spring Boot + Cloud | Zuul Proxy | Integration testing
问题描述
与Spring Boot一起构建微服务时,很容易编写广泛且易读的集成测试,并使用MockRestServiceServer
模拟远程服务请求.
When working with Spring Boot to build micro-services its very easy to write extensive and very readable integration tests and mock remote service requests with MockRestServiceServer
.
是否可以使用类似的方法对ZuulProxy
执行附加的集成测试?我想要实现的是能够模拟ZuulProxy
将转发到的远程服务器,并验证我的所有ZuulFitler
的行为均符合预期.但是,ZuulProxy
使用的是Netflix中的RestClient
(似乎已弃用?),它自然不使用RestTemplate
,它可以由MockRestServiceServer
重新配置,我目前找不到模拟响应的好方法来自远程服务以处理代理请求.
Is there a way to use similar approach to perform additional integration test on ZuulProxy
? What I would like to achieve is being able to mock remote servers that ZuulProxy
would forward to and validate that all of my ZuulFitler
s behaved as expected. However, ZuulProxy
is using RestClient
from Netflix (deprecated it would seem?) which naturally does not use RestTemplate
which could be re-configured by MockRestServiceServer
and I currently can't find a good way of mocking responses from remote services for proxied requests.
我有一个微服务,负责处理API会话密钥的创建,然后其作用类似于API网关.使用Zuul代理转发到基础公开的服务,并且Zuul过滤器将检测会话密钥是否有效.因此,集成测试会创建一个有效的会话,然后转发到伪造的端点,例如集成/测试".
I have a micro-service that is responsible for handling API Session Key creation and then will act similar to an API Gateway. Forwarding is done with Zuul Proxy to underlying exposed services, and Zuul Filters will detect if Session key is valid or not. An integration test would therefore create a valid session and then forward to a fake endpoint, e.g 'integration/test'.
可以通过在@WebIntegrationTest
上设置配置属性来指定集成/测试"是一个新端点,我可以成功模拟通过RestTemplate
处理的所有服务,但不能成功模拟Zuul转发.
Specifying that 'integration/test' is a new endpoint is possible by setting a configuration property on @WebIntegrationTest
, I can successfully mock all services that are being handled via RestTemplate
but not Zuul forwarding.
实现模拟前向目标服务的最佳方法是什么?
What's the best way to do achieve mocking of a forward target service?
推荐答案
查看 WireMock .我一直在用它来对Spring Cloud Zuul项目进行集成级别测试.
Check out WireMock. I have been using it to do integration level testing of my Spring Cloud Zuul project.
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")));
}
}
这篇关于Spring Boot +云| Zuul代理|整合测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!