Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 857
Tuco Avatar asked Mar 07 '23 18:03

Tuco


2 Answers

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

like image 199
ervidio Avatar answered Mar 10 '23 11:03

ervidio


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

like image 26
xross Avatar answered Mar 10 '23 09:03

xross