You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When sending multiple async requests via RestTemplate using CompletableFuture, and when ignoring any exceptions (eg collecting only the requests that have been successful), the MockRestServiceServer.verify() method will not let the @Test fail!
Example:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvcpublicclassMockRestServiceServerTest {
@AutowiredprivateMockMvcmvc;
@AutowiredprivateRestTemplaterestTemplate;
privateMockRestServiceServermockServer;
@Beforepublicvoidinitmock() {
this.mockServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build();
}
@Afterpublicvoidverify() {
//this should let the test fail, but does notmockServer.verify();
}
@Testpublicvoidtest() throwsException {
mockServer.expect(once(), requestTo("/remoteurl")).andRespond(withSuccess());
mvc.perform(MockMvcRequestBuilders
.post("/test"))
.andExpect(status().isOk());
}
}
The rest template sends 3 requests out, and 2 exceptions are logged (because we set up the mock with once().
So far so good, but the MockRestServiceServer seems not to record the failed requests, and .verify() always passes! But it should fail because 3 requests have been send, instead of the expected one only.
Affects: 5.0.8
The text was updated successfully, but these errors were encountered:
Arguably verify() should fail if requests were made to a known URL but were rejected as unexpected. Normally of course the error should surface during request handling, unless it is ignored as in your case. So I'm scheduling this as an improvement.
As an aside this controller method looks like a prime candidate for using the WebClient. I don't know what your actual controller looks like, but based on the given sample code, it becomes something like this:
The return value from the controller is asynchronous too which means the handling is decoupled from the Servlet container thread, and not holding it up.
member sound opened SPR-17266 and commented
When sending multiple async requests via
RestTemplate
usingCompletableFuture
, and when ignoring any exceptions (eg collecting only the requests that have been successful), theMockRestServiceServer.verify()
method will not let the@Test
fail!Example:
The rest template sends 3 requests out, and 2 exceptions are logged (because we set up the mock with
once()
.So far so good, but the
MockRestServiceServer
seems not to record the failed requests, and.verify()
always passes! But it should fail because 3 requests have been send, instead of the expected one only.Affects: 5.0.8
The text was updated successfully, but these errors were encountered: