I have an orchestrator spring boot service that makes several async rest requests to external services and I would like to mock the responses of those services.
My code is:
mockServer.expect(requestTo("http://localhost/retrieveBook/book1"))
.andExpect(method(HttpMethod.GET))
.andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK)
.body("{\"book\":{\"title\":\"xxx\",\"year\":\"2000\"}}")
.contentType(MediaType.APPLICATION_JSON));
mockServer.expect(requestTo("http://localhost/retrieveFilm/film1"))
.andExpect(method(HttpMethod.GET))
.andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK)
.body("{\"film\":{\"title\":\"yyy\",\"year\":\"1900\"}}")
.contentType(MediaType.APPLICATION_JSON));
service.retrieveBookAndFilm(book1,film1);
mockServer.verify();
The retrieveBookAndFilm service calls two methods asynchronous one to retrieve the book and the other to retrieve the film, the problem is that sometimes the films service is executed first and I get an error:
java.util.concurrent.ExecutionException: java.lang.AssertionError: Request URI expected:http://localhost/retrieveBook/book1 but was:http://localhost/retrieveFilm/film1
Any idea how can I solve this issue, is there something similar to mockito to say when this url is executed then return this or that?
Thanks Regards
I ran into the same issue and found it was caused by two things
MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build()
mockServer.expect(ExpectedCount.manyTimes(),
MockRestRequestMatchers.requestTo(myUrl))
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
.andRespond(createResponse())
I found the solution through the combination these two other answers which might offer more information.
How to use MockRestServiceServer with multiple URLs?
Spring MockRestServiceServer handling multiple requests to the same URI (auto-discovery)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With