2016-08-09 4 views
6

@RequestParamアノテーションを持つパラメータは:post( "/ ******/***")。param( "variable"、 "value" )MockMVCを使用してコントローラの@RequestBodyパラメータを渡す方法

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

エラーは次のとおりです。 enter image description here どのように私はのためのモック値を渡すことができますここで資格情報

+1

申し訳ありませんあなたは何を示唆していませんでした – dexter

+0

申し訳ありませんが、私は質問を理解していませんが、模擬の中で '@ RequestBody'を渡す方法を尋ねているなら、あなたの解決策はあなたの例です:' mockkMvc。実行する( 投稿( "/ usermgmt/creat param( "username"、 "aricloud_admin")。contentType( MediaType.APPLICATION_JSON).content(json)) '。これを前にjsonに変換しました。 'String json = gson.toJson(cloudCredentialsBean);'そして 'content(json)'でコンテンツを渡しました。これは、あなたの望むことですか? – Pau

+0

@PauChorroしかし、これは動作していません – dexter

答えて

7

通常、POSTリクエストはその本体にそのパラメータを渡します。だから私は同じ要求に対して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()); 
+0

私は間違いを犯しました、私は" userId "を" username " () – dexter

関連する問題