Java – how to use mockmvc to pass the @ requestbody parameter of the controller
•
Java
Parameters with @ requestparam annotation can use: Post ("/ ************************************************************ Param ("variable", "value") pass
But how do I pass parameter values with the @ 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());
}
The controller method 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);
}
The error I get is: how do I pass the simulated value of the voucher here?
Solution
A post request usually passes its parameters in its body So I can't understand your expectations by providing parameters and content in the 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 explicitly pass it as a query string:
mockmvc.perform(
post("/usermgmt/createCloudCredential?username=aricloud_admin")
.contentType(MediaType.APPLICATION_JSON).content(json))
.andExpect(status().isOk());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
