2013-07-31 59 views
32

我使用彈簧3.2模擬MVC來測試我controller.My代碼模擬MVC - 添加請求參數測試

@Autowired 
    private Client client; 

    @RequestMapping(value = "/user", method = RequestMethod.GET) 
     public String initUserSearchForm(ModelMap modelMap) { 
      User user = new User(); 
      modelMap.addAttribute("User", user); 
      return "user"; 
     } 

     @RequestMapping(value = "/byName", method = RequestMethod.GET) 
     @ResponseStatus(HttpStatus.OK) 
     public 
     @ResponseBody 
     String getUserByName(@RequestParam("firstName") String firstName, 
           @RequestParam("lastName") String lastName, @ModelAttribute("userClientObject") UserClient userClient) { 

      return client.getUserByName(userClient, firstName, lastName); 
     } 

,我寫了下面的測試:

@Test 
public void testGetUserByName() throws Exception { 
     String firstName = "Jack"; 
     String lastName = "s";  
     this.userClientObject = client.createClient(); 
     mockMvc.perform(get("/byName") 
       .sessionAttr("userClientObject", this.userClientObject) 
       .param("firstName", firstName) 
       .param("lastName", lastName)    
     ).andExpect(status().isOk()) 
       .andExpect(content().contentType("application/json")) 
       .andExpect(jsonPath("$[0].id").exists()) 
       .andExpect(jsonPath("$[0].fn").value("Marge")); 

} 

我所得到的是

java.lang.AssertionError: Status expected:<200> but was:<400> 
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60) 
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89) 
    at org.springframework.test.web.servlet.result.StatusResultMatchers$5.match(StatusResultMatchers.java:546) 
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141) 

爲什麼會發生這種情況?是它傳遞@RequestParam

回答

45

當我分析你的代碼。我也面臨同樣的問題,但我的問題是,如果我給第一個和最後一個名字的價值意味着它工作正常。但是,當我只給出一個值,意味着它說400反正使用.andDo(打印())方法來找出錯誤

public void testGetUserByName() throws Exception { 
    String firstName = "Jack"; 
    String lastName = "s";  
    this.userClientObject = client.createClient(); 
    mockMvc.perform(get("/byName") 
      .sessionAttr("userClientObject", this.userClientObject) 
      .param("firstName", firstName) 
      .param("lastName", lastName)    
    ).andDo(print()) 
    .andExpect(status().isOk()) 
      .andExpect(content().contentType("application/json")) 
      .andExpect(jsonPath("$[0].id").exists()) 
      .andExpect(jsonPath("$[0].fn").value("Marge")); 
} 

如果你的問題是org.springframework.web.bind.missingservletrequestparameterexception你有你的代碼更改

@RequestMapping(value = "/byName", method = RequestMethod.GET) 
    @ResponseStatus(HttpStatus.OK) 
    public 
    @ResponseBody 
    String getUserByName(@RequestParam(value="firstName",required = false) String firstName, 
          @RequestParam(value="lastName",required = false) String lastName, @ModelAttribute("userClientObject") UserClient userClient) { 

     return client.getUserByName(userClient, firstName, lastName); 
    } 
+0

我寫了相同的代碼,但是有問題的ModelAttribute混淆。請閱讀http://stackoverflow.com/questions/19427341/why-modelatribute-passed-as-null – gstackoverflow

+2

很好的前端與'andDo(打印())',非常實用! –

+3

記事,'打印()''是MockMvcResultHandlers'類的方法 – WildDev

0

@ModelAttribute正確的方式被的請求參數的彈簧映射到特定對象類型。所以你的參數可能看起來像userClient.usernameuserClient.firstName等作爲MockMvc模仿從瀏覽器的請求,你需要在春季將使用從形式到實際建設UserClient對象的參數傳遞。

(我認爲的ModelAttribute的是一種幫助構建從一堆打算從一種形式進來字段的對象,但你可能想要做一些閱讀,以獲得更好的定義)

+0

我用的ModelAttribute之前,這不是一個疑難問題都與如何發送@RequestParam – jackyesind