title: Mock MVC - Add Request Parameter to test
body: <p>I am using spring 3.2 mock mvc to test my controller.My code is</p>
  
  <pre><code> @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);
          }
  </code></pre>
  
  <p>and I wrote following test:</p>
  
  <pre><code>@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"));
  }
  </code></pre>
  
  <p>what i get is </p>
  
  <pre><code>java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;400&gt;
      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)
  </code></pre>
  
  <p>Why this happens? Is it right way to pass the @RequestParam</p>