I'm trying to convert Spring REST Controller into async mode, but I have problems running tests using MockMvc
. The test passes, but it does not rollback DB changes, so I end up with test data in DB. When using synchronous controller, data gets rolled back correctly.
This is my controller method:
@RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public Callable<ResponseEntity<Void>> userSignup(@RequestBody User user, HttpServletResponse response, HttpServletRequest request) {
return () -> {
String id = userService.createUser(user);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(new URI(findServerUrlFromRequest(request, "/users/id/" + id)));
return new ResponseEntity<>(headers, HttpStatus.CREATED);
};
}
And this is the test:
@Test
@Rollback(true)
public void createUserTest() throws Exception{
User user = new User();
user.setFirstname("John");
user.setLastname("Doe");
user.setEmail("user@mail.com");
user.setPassword("password");
JAXBContext ctx = JAXBContext.newInstance(User.class);
Marshaller marshaller = ctx.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(user, writer);
writer.close();
System.out.println("XML: " + writer.toString());
MvcResult result = mockMvc.perform(post("/users").content(writer.toString()).contentType("application/xml")).andExpect(request().asyncStarted()).andReturn();
mockMvc.perform(asyncDispatch(result)).andExpect(status().isCreated()).andExpect(header().string("Location",org.hamcrest.Matchers.startsWith("http://localhost:80/users/id"))).andDo(print()).andReturn();
}
I'm thinking it's some kind of configuration issue, but can't figure out what is the problem. Can anybody give me a hint?