Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock multiple responses for same request using spring's MockRestServiceServer?

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?

like image 651
Sjoerd During Avatar asked Jul 16 '18 14:07

Sjoerd During


1 Answers

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());
}
like image 76
Sjoerd During Avatar answered Oct 14 '22 07:10

Sjoerd During