简体   繁体   English

如何使用MockMVC传递控制器的@RequestBody参数

[英]How to pass @RequestBody parameter of controller using MockMVC

Parameters with @RequestParam annotation can be passed by using : post("/******/***").param("variable", "value") 带有@RequestParam注释的参数可以使用:post(“/ ****** / ***”)。param(“variable”,“value”)传递

but how can I pass value of parameter having @RequestBody annotation? 但是如何传递具有@RequestBody注释的参数值?

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? 如何在此处传递凭证的模拟值?

A POST request normally passes its param in its body. POST请求通常会在其正文中传递其param So I cannot understand what you expect by giving both a param and a content for same request. 所以我无法通过同一个请求给出一个param和一个content来理解你的期望。

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: 如果需要传递参数"username=aricloud_admin" ,请将其添加到JSON字符串,或者将其作为查询字符串显式传递:

    mockMvc.perform(
        post("/usermgmt/createCloudCredential?username=aricloud_admin")
            .contentType(MediaType.APPLICATION_JSON).content(json))
            .andExpect(status().isOk());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM