spring - Mock MVC - Add Request Parameter to test -


i using spring 3.2 mock mvc test controller.my code is

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

and wrote following test:

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

what

java.lang.assertionerror: status expected:<200> was:<400>     @ org.springframework.test.util.assertionerrors.fail(assertionerrors.java:60)     @ org.springframework.test.util.assertionerrors.assertequals(assertionerrors.java:89)     @ org.springframework.test.web.servlet.result.statusresultmatchers$5.match(statusresultmatchers.java:546)     @ org.springframework.test.web.servlet.mockmvc$1.andexpect(mockmvc.java:141) 

why happens? right way pass @requestparam

when analyzed code. have faced same problem problem if give value both first , last name means working fine. when give 1 value means says 400. anyway use .anddo(print()) method find out error

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

if problem org.springframework.web.bind.missingservletrequestparameterexception have change code

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

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -