I am using the MockMvcResultMathchers to test my async function but it not work properly.
Here is my function :
@GetMapping(
value = "/v1.0/sms-booking/async-handle",
produces = {"text/plain"})
@Async
public CompletableFuture<ResponseEntity<String>> smsAsyncHandle(@RequestParam("moLogID ") Integer moLogID ) {
CompletableFuture.runAsync(...);
return CompletableFuture.completedFuture(
ResponseEntity.ok("New Message Received with moLogID " + moLogID + " !"));
}
and the test function
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureObservability
@AutoConfigureMockMvc
@EnableAsync
class SmsAsyncControllerTest {
@Test
void smsAsyncHandle_returnMoLogID_whenValidParam() throws Exception {
MvcResult mvcResult = mockMvc.perform(get(SMS_ASYNC_HANDLE_PATH)
.param("moLogID","1")
.andDo(print()).andExpect(request().asyncStarted())
.andReturn();
mockMvc.perform(asyncDispatch(mvcResult))
.andDo(print())
.andExpect(request().asyncResult(instanceOf(ResponseEntity.class)))
.andExpect(content().string(containsString("1")));
}
}
and when I run test, this is the result.
enter image description here
I want my test function run successfully. I have tried many different ways I found on internet but nothing works. Pls help me to overcome this.
Thanks