How to pass @RequestBody parameter of controller using MockMVC

dexter :

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 : enter image description here How can I pass a mock value for credential here?

Serge Ballesta :

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());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MockMvc - Pass an object from the mock to the controller

When using MockMvc to test the controller, a parameter passing error occurred

How to pass multiple parameters to @RequestBody using JavaScript XMLHttpRequest

How to pass MultipartFile in requestBody for a spring boot rest controller

How to pass parameter to FormType constructor from controller

Pass parameter to controller

how to treat controller exception with mockmvc

How to test a controller with constructor injection by MockMvc

how to pass parameter with @url.action to controller

How to enable controller parameter validation for standalone MockMvc

How to pass parameter to controller function

How to pass a div element as a parameter to an MVC controller?

How to pass a parameter to a Mobx Controller (Flutter)

CakePHP: Using FormHelper to pass search query to controller parameter

How to pass a parameter (that identify the primery key of an object) from the view to a controller using Spring MVC?

How to pass parameter from ActionLink to controller at runtime

Pass parameter to Spring controller using @PathVariable

How to pass two forms as parameter to laravel controller?

How to pass 2 objects in @RequestBody?

How can I pass a object using MockMvc as a RequestBody?

How to pass an object to a ModelAttrbiute in MockMVC post?

how to pass two parameter in route controller

How to pass output parameter in a controller in an WebAPI

how to pass model as parameter to controller

Laravel - How to pass parameter from controller to route and use it in another controller?

NodeJS - How to pass url parameter to controller function?

how to pass object as a parameter to the mockmvc?

How to pass parameter using JavaScript to MVC Controller - ASP.NET 7

How to pass correctly parameter from view to controller?