Tuco
Tuco

Reputation: 1052

Passing query parameters to TestRestTemplate

Hi I'm using TestRestTemplate to implement some integration tests for my code and while trying to test an endpoint I can't get find a way to include the query params.

Here are 2 different tests I've tried:

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

And here's the controller I'm testing:

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

Basically when I debug the code, whenever the request reaches the endpoint the params I tried sending through the test are somehow empty so filters is null and Pageable I'm guessing uses default values because it sets them to page=0 and size=20. I tried using the .exchange(...), .getForEntity(...) and .getForObject(...) methods from the TestRestTemplate class, but none seem to work with query params, could someone please help me out and tel me what I might be doing wrong, I'd really appreciate it!

Upvotes: 6

Views: 13313

Answers (2)

xross
xross

Reputation: 648

I personally think this should be this way: I am using TestRestTemplate to Test with @RequestParam value how to execute

I am leaving the link here if somebody is looking for the correct answer

Upvotes: 1

ervidio
ervidio

Reputation: 591

It looks like your problem is that you are not including the params in your URL. It should be something like:

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

Please, find in the following link some examples just in case it can help you

Upvotes: 9

Related Questions