2017-10-23 29 views
1

こんにちは私はTestRestTemplateを使用してコードの統合テストを実装していますが、エンドポイントをテストしようとしている間はクエリパラメータを含める方法が見つかりません。TestRestTemplateにクエリパラメータを渡す

@Test 
@DisplayName("Test list all filtered by boolean field") 
void testListAllBooleanFilter() { 
    Map<String, String> params = new HashMap<>(); 
    params.put("page", "0"); 
    params.put("size", "5"); 
    params.put("filters", "active=true"); 
    ResponseEntity<AdminDTO[]> response = this.testRestTemplate.getForEntity("/api/v1/admin", AdminDTO[].class, 
      params); 
    assertThat(response.getBody()).isNotNull(); 
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); 
    assertThat(response.getBody()).hasSize(2); 
    assertThat(response.getBody()[0].getActive()).isTrue(); 
    assertThat(response.getBody()[1].getActive()).isTrue(); 
} 

@Test 
@DisplayName("Test list all with empty result") 
void testListAllEmptyResult() { 
    HttpEntity<String> requestEntity = new HttpEntity<>(new HttpHeaders()); 
    Map<String, String> params = new HashMap<>(); 
    params.put("page", "0"); 
    params.put("size", "5"); 
    params.put("filters", "active=false"); 
    ResponseEntity<List> response = this.testRestTemplate.exchange("/api/v1/admin", HttpMethod.GET, 
      requestEntity, List.class, params); 
    assertThat(response.getBody()).isNotNull(); 
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); 
    assertThat(response.getBody()).isEmpty(); 
} 

そして、ここで私がテストだコントローラの:

ここで私が試した2種類のテストがある。基本的

@GetMapping(value = "/admin", produces = "application/json") 
public ResponseEntity listAll(String filters, Pageable pageable) { 
    if(filters == null) { 
     filters = "type=" + ADMIN.toString(); 
    } else { 
     filters += ",type=" + ADMIN.toString(); 
    } 
    Condition condition = filterMapService.mapFilterToCondition("user_account", filters); 
    List<AdminDTO> adminAccounts = userAccountRepository.findAllByFilter(condition, pageable); 
    if (adminAccounts.isEmpty()) { 
     return new ResponseEntity<>(adminAccounts, HttpStatus.OK); 
    } 
    return new ResponseEntity<>(adminAccounts, HttpStatus.OK); 
} 

私は、要求がエンドポイントに到達するたびにコードをデバッグするとき私がテストを通して送信しようとしたパラメータは何とか空ですので、フィルタはnullであり、Pageablepage=0size=20に設定されているので、デフォルト値を使用していると思います。私はTestRestTemplateクラスの.exchange(...),.getForEntity(...)および.getForObject(...)メソッドを使用してみましたが、クエリパラメータではうまくいかないようです。誰かが私を助けてくれて、私が間違っているかもしれないと私に本当に感謝しています。

答えて

1

あなたの問題は、あなたのURLにparamsを含めていないということです。 ??のparamsは、 `/ API/V1 /管理ページサイズのようにする必要があります場合でも、念のために、それはあなたの

+0

を助けることができる

/api/v1/admin?page={page}&size={size}&filters={filters} 

は、以下のlinkいくつかの例で見つけてください:それは何かのようにする必要があります?filters'? – Tuco

+1

'/ api/v1/admin?page = {page}&size = {size}&filters = {filters}'のようになります。 – Tuco

+0

偉大な、私は私の答えを更新しました。ご確認いただきありがとうございます :) – ervidio

関連する問題