0

I have the following code:

@Component
public class ProductServiceClient {
    private final RestTemplate template;

    public ProductServiceClient(RestTemplate template) {
        this.template = template;
    }

    public HttpStatus retrieveProducts() {
        HttpStatus response = null;
        try {
            response = template.getForEntity("/products", ProductContainer.class);
        } catch (RestClientResponseException exception) {
            responseStatus = getAllStat(exception);
        }
        return response;
    }

    private HttpStatus getAllStat (RestClientResponseException exception) {
        HttpStatus responseStatus = null;

        var getResponse = objectMapper.readValue(exception.getResponseBodyAsByteArray(), ProductContainer.class);
        responseStatus = getResponse.getCode();

        return responseStatus;
    }

}

I am having trouble with testing private method getAllstat (it can't be tested directly, only through retrieveProducts() method). I need to cover exactly that method with test. Here what I have by now:

@Test
class GetStatusOfProducts {
RestTemplate template = new RestTemplate();
when(restTemplate.postForEntity("/products", ProductContainer.class).thenThrow(
            RestClientResponseException.class);
}
Stack8
  • 1
  • 1
  • `new RestTemplate()` is not a mock. As such, its methods cannot be mocked. Also, your `@Test` method is not testing anything, it is only trying to set up a mock method. But it is neither calling a method of your controller, nor is it asserting anything. – knittl Oct 17 '22 at 17:46
  • @knittl In real test it is mocked, that's just simple example – Stack8 Oct 18 '22 at 06:24
  • Then please [edit] your question to include a [mre]. Because right now the answer is "it is not a mock, this cannot work". – knittl Oct 18 '22 at 06:26

0 Answers0