8

Im using MockRestServiceServer for mocking http responses. In a specific scenario i call an endpoint two times and want a different response the second time.

But when i write a second expectation it's like it overwrites my first expectation.

How does one write multiple responses for the same request?

Sjoerd During
  • 121
  • 1
  • 5

2 Answers2

4

I've found it after some research:

When instantiating a MockRestServiceServer it default gets an UnorderedRequestExpectationManager. Changing this via the Builder in a SimpleRequestExpectationManager adds support for adding multiple responses in the order of defining them.

private MockRestServiceServer createMockServerBy(Class<? extends 
RestTemplate> requiredType) {
    RestTemplate template = context.getBean(requiredType);
    return MockRestServiceServer.bindTo(template).build(new 
    SimpleRequestExpectationManager());
}
Sjoerd During
  • 121
  • 1
  • 5
  • I am actually finding that `SimpleRequestExpectationManager` *is* the default unless I call `ignoreExpectOrder(true)` after `bindTo()` – matt forsythe Dec 29 '20 at 22:57
0

Have you tried WireMock? It's amazing and provides many features to mock APIs. Have a look at http://wiremock.org/

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54