java.lang.AssertionError:预期状态:<;200>;但事实是:<;400>;

java.lang.AssertionError:预期状态:<;200>;但事实是:<;400>;,java,spring,spring-mvc,Java,Spring,Spring Mvc,是我的控制器 @RequestMapping(value = "/user", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE, headers = "Accept=application/json") public @ResponseBody ResponseMessage getUser(@RequestB

是我的控制器

@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);
}
@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);
}
这是我的测试控制器

@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);
}
@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);
}
@测试
public void testgetUser()引发异常
{
AvailableUser AvailableUser=新的AvailableUser();
List lst=new ArrayList();
第1条添加(“测试1”);
第1条添加(“测试2”);
可用的用户setUuId(lst);
this.mockMvc.perform(post(“/user”).contentType(MediaType.APPLICATION\u JSON).accept(MediaType.APPLICATION\u JSON))
.andExpect(状态().isCreated())
.andExpect(status().isOk());
当(Manager.availableUser(availableUser)),然后返回(消息);
}
当控制器方法调用
(“/user”)
表单
testcontroller
时,我不知道如何传递对象


我收到了错误消息
java.lang.AssertionError:Status expected:但是是:

如果您使用的是Jackson,最简单的方法是使用
ObjectMapper
的实例将
AvailableUser
序列化为JSON字符串:

@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
}

也许是因为你没有按你的要求提供任何尸体。尝试将.content(availableUser)添加到您的帖子当我添加{.content(availableUser)}时,我得到了错误消息。“MockHttpServletRequestBuilder类型中的方法内容(字节[])不适用于参数(availableUser)”“我不明白为什么在控制器中的头中添加“Accept”?当我们有消费/生产时,这是必需的吗?