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
MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build()
- it helped a lot! – Sportsmanship