簡體   English   中英

java.lang.AssertionError: 預期狀態:<200> 但為:<400>

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

這是我的控制器...

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

當控制器方法調用("/user")表單testcontroller時,我不知道如何傳遞對象。

我收到錯誤消息java.lang.AssertionError: Status expected:<200> but was:<400>

如果您使用 Jackson,最簡單的方法是使用ObjectMapper的實例將AvailableUser序列化為 JSON String:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM