简体   繁体   中英

java.lang.AssertionError: Status expected:<200> but was:<400>

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>

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM