How to use MockRestServiceServer with multiple URLs?

那年仲夏 提交于 2019-12-11 03:25:08

问题


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

  1. Call URL #1
  2. Call URL #1 (for a second time)
  3. Call URL #2

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

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

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?


回答1:


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.

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



来源:https://stackoverflow.com/questions/40495531/how-to-use-mockrestserviceserver-with-multiple-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!