Java mvc传递参数_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 getArgumentCaptor =

ArgumentCaptor.forClass(String.class);

ArgumentCaptor getInteger = ArgumentCaptor.forClass(Integer.class);

ArgumentCaptor getArgumentCaptorCredential =

ArgumentCaptor.forClass(CloudCredentialsBean.class);

when(

userManagementHelper.createCloudCredential(getInteger.capture(),

getArgumentCaptorCredential.capture())).thenReturn(

new ResponseEntity(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 createCloudCredential(

@RequestParam("userId") int userId,

@RequestBody CloudCredentialsBean credential) {

return userManagementHepler.createCloudCredential(userId, credential);

}

我得到的错误是:

7nAeB.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值