I need to test a controller which calls an asynchronous service.
CONTROLLER CODE
@RequestMapping(value = "/path", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Result> massiveImport(HttpServletRequest request) {
try {
service.asyncMethod(request);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(new Result(e.getMessage()), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(new Result(saveContact.toString()), HttpStatus.OK);
}
SERVICE CODE
@Async
public Future<Integer> asyncMethod(HttpServletRequest request) throws IllegalFieldValueException, Exception {
...
return new AsyncResult<>(value);
}
TEST CODE
MvcResult result = getMvc().perform(MockMvcRequestBuilders.fileUpload("/path/")
.header("X-Auth-Token", accessToken)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andReturn();
Test is ok. But I would wait, before closing the test, to complete async service.
Is there any way to do this?