微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

带有@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());

    }

正在测试的控制器方法是:

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

我得到的错误是:

如何在此处传递凭证的模拟值?

解决方法

POST请求通常会在其正文中传递其参数.所以我无法通过同一个请求提供参数和内容来理解您的期望.

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

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

原文地址:https://www.jb51.cc/java/120869.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐