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?
MockMvc provides support for Spring MVC testing. It encapsulates all web application beans and makes them available for testing. We'll initialize the mockMvc object in the @BeforeEach annotated method, so that we don't have to initialize it inside every test.
MockMvc is defined as a main entry point for server-side Spring MVC testing. Tests with MockMvc lie somewhere between between unit and integration tests.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With