Reputation: 49
it's my controller...
@RequestMapping(value = "/user", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE, headers = "Accept=application/json")
public @ResponseBody ResponseMessage getUser(@RequestBody AvailableUser uuid) {
logger.info("enter into getuser method's body");
return Manager.availableUser(uuid);
}
it's my testcontroller...
@Test
public void testgetUser() throws Exception
{
AvailableUser availableUser=new AvailableUser();
List<String> lst =new ArrayList<String>();
lst.add("test1");
lst.add("test2");
availableUser.setUuId(lst);
this.mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(status().isOk());
when(Manager.availableUser(availableUser)).thenReturn(message);
}
I don't know how to pass the object when controller method call ("/user")
form testcontroller
.
and I got error message java.lang.AssertionError: Status expected:<200> but was:<400>
Upvotes: 2
Views: 12417
Reputation: 48193
If you're using Jackson, the simplest approach is to serialize the AvailableUser
to JSON String using an instance of ObjectMapper
:
@Test
public void testgetUser() throws Exception
{
// Same stuff
ObjectMapper mapper = new ObjectMapper();
this.mockMvc
.perform(
post("/user")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(availableUser))
)
.andExpect(status().isCreated())
.andExpect(status().isOk());
// Same as before
}
Upvotes: 2