I\'m doing an integration test against a REST controller POST handler. Well, I\'m trying to.
It gives me the HttpMessageNotReadableException exception: Could not rea
In my opinion the problem is with content format. Your endpoint expect that data will be sent as application/json but in test you send it as application/x-www-form-urlencoded (it doesn't matter that you set proper content type header in request). Try to send admin object in json format (as a body of a request):
{
"firstname" : "Stephane",
"lastname" : "Eybert",
"login" : "stephane",
"password" : "toto"
}
BTW the /admin/crud
does not fallow REST resource addressing rules, you should change it to /admin
. The crud (CREATE, READ, UPDATE, DELETE) will map to HTTP methods (POST, GET, PUT, DELETE)
I replaced the .param() methods in favor of the .content() one:
post("/admin/crud").headers(httpHeaders)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{ \"firstname\" : \"" + admin0.getFirstname() + "\", \"lastname\" : \"" + admin0.getLastname() + "\", \"email\" : \"" + admin0.getEmail() + "\", \"login\" : \"" + admin0.getLogin() + "\", \"password\" : \"" + admin0.getPassword() + "\", \"passwordSalt\" : \"" + admin0.getPasswordSalt() + "\" }")
).andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.firstname").value(admin0.getFirstname()))
.andExpect(jsonPath("$.lastname").value(admin0.getLastname()))
.andExpect(jsonPath("$.email").value(admin0.getEmail()))
.andExpect(jsonPath("$.login").value(admin0.getLogin()))
.andExpect(jsonPath("$.password").value(admin0.getPassword()))
.andExpect(jsonPath("$.passwordSalt").value(admin0.getPasswordSalt()))
.andReturn();
And it now works as expected.