java - 如何使用 MockMVC 传递 Controller 的 @RequestBody 参数

标签 java spring-mvc junit mockito

带有@RequestParam注解的参数可以通过使用:post("/******/***").param("variable", "value")

但是如何传递带有@RequestBody 注解的参数值呢?

我的测试方法是:

@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 方法是:

@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);
        }   

我得到的错误是: enter image description here 如何在此处传递 credential 的模拟值?

最佳答案

POST 请求通常在其正文中传递其 param。因此,我无法通过为同一请求同时提供 paramcontent 来理解您的期望。

所以在这里,你可以简单地做:

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

如果您需要传递参数"username=aricloud_admin",请将其添加到JSON 字符串中,或​​者将其作为查询字符串显式传递:

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

关于java - 如何使用 MockMVC 传递 Controller 的 @RequestBody 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38843218/

相关文章:

java - ONOS 中的 SSL 调试

java - Prepared Statements 额外引号

java - Spring Boot 应用程序错误

spring - Cucumber-spring 找不到步骤定义

java - 传递来验证的参数不是 Mock

java - 测试线程程序

System.out 中的 Java 执行细节

java - Spring mvc 并将列表作为表单支持对象进行迭代

spring-mvc - Spring HashMap 表单

java - 我应该为以下功能编写哪些单元测试用例?另外,如何提供示例 JSONobject 作为函数中的参数?