我需要对MockRestServiceServer的一个实例配置多个期望。对两个不同的URL的期望是:
同一个URL被调用两次,然后对同一个具有不同请求参数的URL进行第三次调用。
我有一个负载平衡RestTemplate的实例可以插入到我的测试中,并将其传递给MockRestServiceServer.createServer()。
我尝试将这3种期望内联到我的MockRestServiceServer实例中,但是测试失败了,声称第三个URL是预期的,但是它看到了第一个。看起来,我不是在覆盖期望,就是有一些有状态的共享,使模拟服务器处于错误的状态。
有人能给我举个例子说明如何正确地做到这一点吗?
发布于 2022-04-26 07:07:11
万一有人听不懂@alex.b回复
// Create a mock server with UnorderedRequestExpectationManager
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build(new UnorderedRequestExpectationManager());
// Add multiple rest url
mockServer.expect(ExpectedCount.once(),
requestTo(URL1))
.andExpect(method(HttpMethod.GET))
.andRespond(withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body(result1));
mockServer.expect(ExpectedCount.once(),
requestTo(URL2))
.andExpect(method(HttpMethod.GET))
.andRespond(withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body(result2));
// Add as much as you want发布于 2017-11-16 10:31:00
如果在Mock Server org.springframework.test.web.client.MockRestServiceServer#MockRestServiceServer中使用非默认期望管理器,您的问题就可以解决:它接受org.springframework.test.web.client.RequestExpectationManager的一个参数。
您可以传递以下类型:org.springframework.test.web.client.UnorderedRequestExpectationManager
https://stackoverflow.com/questions/40495531
复制相似问题