How to pass @RequestBody parameter of controller using MockMVC
34,215
A POST
request normally passes its param
in its body. So I cannot understand what you expect by giving both a param
and a content
for same request.
So here, you can simply do:
mockMvc.perform(
post("/usermgmt/createCloudCredential").contentType(
MediaType.APPLICATION_JSON).content(json)).andExpect(
status().isOk());
If you need to pass the parameter "username=aricloud_admin"
, add it to the JSON
string, or alternatively, pass it explicitly as a query string:
mockMvc.perform(
post("/usermgmt/createCloudCredential?username=aricloud_admin")
.contentType(MediaType.APPLICATION_JSON).content(json))
.andExpect(status().isOk());
Author by
dexter
Updated on October 29, 2020Comments
-
dexter over 3 years
Parameters with @RequestParam annotation can be passed by using : post("/******/***").param("variable", "value")
but how can I pass value of parameter having @RequestBody annotation?
My test method is :
@Test public void testCreateCloudCredential() throws Exception { CloudCredentialsBean cloudCredentialsBean = new CloudCredentialsBean(); cloudCredentialsBean.setCloudType("cloudstack"); cloudCredentialsBean.setEndPoint("cloudstackendPoint"); cloudCredentialsBean.setUserName("cloudstackuserName"); cloudCredentialsBean.setPassword("cloudstackpassword"); cloudCredentialsBean.setProviderCredential("cloudstackproviderCredential"); cloudCredentialsBean.setProviderIdentity("cloudstackproviderIdentity"); cloudCredentialsBean.setProviderName("cloudstackproviderName"); cloudCredentialsBean.setTenantId(78); cloudCredentialsBean.setCredentialId(98); StatusBean statusBean = new StatusBean(); statusBean.setCode(200); statusBean.setStatus(Constants.SUCCESS); statusBean.setMessage("Credential Created Successfully"); Gson gson = new Gson(); String json = gson.toJson(cloudCredentialsBean); ArgumentCaptor<String> getArgumentCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<Integer> getInteger = ArgumentCaptor.forClass(Integer.class); ArgumentCaptor<CloudCredentialsBean> getArgumentCaptorCredential = ArgumentCaptor.forClass(CloudCredentialsBean.class); when( userManagementHelper.createCloudCredential(getInteger.capture(), getArgumentCaptorCredential.capture())).thenReturn( new ResponseEntity<StatusBean>(statusBean, new HttpHeaders(), HttpStatus.OK)); mockMvc.perform( post("/usermgmt/createCloudCredential").param("username", "aricloud_admin").contentType( MediaType.APPLICATION_JSON).content(json)).andExpect( status().isOk()); }
Controller method that is being tested is :
@RequestMapping(value = "/createCloudCredential", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<StatusBean> createCloudCredential( @RequestParam("userId") int userId, @RequestBody CloudCredentialsBean credential) { return userManagementHepler.createCloudCredential(userId, credential); }
Error that I am getting is : How can I pass a mock value for credential here?
-
dexter almost 8 yearsI got the mistake, i have to pass "userId" inplace of "username" in param()