Тестирование RestTemplate junit5 - "ResponseEntity.getBody() because is null"

Есть рабочий метод который дергает рест. При его тестировании junit5 получаю ошибку

public Country getCountry() {
   HttpHeaders headers = new HttpHeaders();
   headers.set(SYSTEM_CODE, "code");
   ResponseEntity<Country> responseEntity = restTemplate.exchange(
            properties.getUrl(),
            HttpMethod.GET,
            new HttpEntity(headers),
            Country.class);
    return responseEntity.getBody();
   }

//testing
Country country = new Country();
country.setId(1);

ResponseEntity<CountryPage> mockEntity = new ResponseEntity<>(country, HttpStatus.OK);

Mockito.when(mockEntity.getBody()).thenReturn(country);

Mockito.when(clientRestTemplate.exchange(
            ArgumentMatchers.anyString(),
            ArgumentMatchers.any(HttpMethod.class),
            ArgumentMatchers.any(),
            ArgumentMatchers.<Class<Country>>any()))
            .thenReturn(mockEntity);

//do
some.getCountry();

результат:

java.lang.NullPointerException: Cannot invoke "org.springframework.http.ResponseEntity.getBody()" because "responseEntity" is null

Какова причина, почему он null когда определен. Находил похожие варианты но ситуация не исправилась.

UP: ошибка вылетает на уровне return responseEntity.getBody();.


Ответы (0 шт):