0

I'm trying write unit tests for the class which has a

restTemplate.getForObject( Url, Response.class);

I've mocked the RestTemplate in the test class still when I run test case, request hit's server and I get error

restTemplate.getForObject(configDiyUrl, BaseResponse.class);

error:

I/O error on GET request for "http://localhost:8080/WMService/api/v1/BulkDataSync": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect

I've mocked the RestTemplate like this,

@Mock
RestTemplate restTemplate

then I've written the when/then mocking like this,

when(restTemplate.getForObject(anyString(), eq(Response.class))).thenReturn(br);

still when I run test case I get above mentioned error.

As per my understanding it should not hit the server when it is mocked.

I'm looking for some help about mocking RestTemplate so that it won't hit server.

I've gone throgh this example still I'm getting error as it hit's server.

  • 1
    Please, add the code of the tested class (especially the place where RestTemplate is declared, e.g. a field) and the complete code of your test (especially the tested class instance with annotations, any `before` methods etc.). – Jonasz Jun 06 '23 at 18:25
  • You haven't shown how your mock is getting injected into the class you are testing. Please look at https://stackoverflow.com/questions/74027324/why-are-my-mocked-methods-not-called-when-executing-a-unit-test – tgdavies Jun 07 '23 at 01:30

0 Answers0