如何将MockRestServiceServer与多个URL一起使用? [英] How to use MockRestServiceServer with multiple URLs?

查看:169
本文介绍了如何将MockRestServiceServer与多个URL一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在MockRestServiceServer实例上配置多个期望.期望使用两个不同的URL:

I need to configure multiple expectations on an instance of MockRestServiceServer. The expectations are for two different URLs:

  1. 呼叫URL#1
  2. 调用URL#1(第二次)
  3. 呼叫URL#2

同一URL被调用两次,然后对具有不同请求参数的同一URL进行第三次调用.

The same URL is called twice, then a 3rd call is made to the same URL with different request params.

我有一个负载均衡的RestTemplate实例可以注入到我的测试中,然后将其传递给MockRestServiceServer.createServer().

I have one instance of a load-balanced RestTemplate available to inject into my test, and I pass this to MockRestServiceServer.createServer().

我尝试将这3个期望值内联到我的MockRestServiceServer实例中,但是测试失败,声称第三个URL是预期的,但它看到了第一个.似乎我是在覆盖期望值,或者在这里共享有状态信息,使模拟服务器处于错误状态.

I've tried to inline these 3 expectations to my MockRestServiceServer instance but the test fails claiming the 3rd URL was expected but it saw the 1st. It seems like I'm either overwriting the expectations or there's something stateful being shared here that's keeping the mock server in the wrong state.

任何人都可以向我展示如何正确执行此操作的示例吗?

Can anyone show me an example of how to do this correctly?

推荐答案

如果在模拟服务器org.springframework.test.web.client.MockRestServiceServer#MockRestServiceServer中不使用默认的期望管理器,则可以解决您的问题:它接受参数org.springframework.test.web.client.RequestExpectationManager.

Your problem can be solved if you use not default expectation manager within the Mock Server org.springframework.test.web.client.MockRestServiceServer#MockRestServiceServer: it accepts a parameter of org.springframework.test.web.client.RequestExpectationManager.

您可以传递以下类型:org.springframework.test.web.client.UnorderedRequestExpectationManager

You can pass this type: org.springframework.test.web.client.UnorderedRequestExpectationManager

这篇关于如何将MockRestServiceServer与多个URL一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆