title: Mockito unit test for rest template
body: <p>I am trying to write a unit test for rest template that is making http calls. I have created the rest template with the rest template builder, shown below. the rest template is set to configure read and connection timeouts. I also have a retry template that is to execute retries when the application has a timeout. I am at the point where I have specified the http methods: postForEntity, exchange, and getForEntity that need to retried in the retry template and need help with writing unit tests. I started with the getForEntity method but am receiving a different output from what is expected. any assistance with this would be helpful.</p>
  
  <p>Rest Template</p>
  
  <pre><code> @Bean
  public RestTemplate restTemplate() {
      return new RestTemplateBuilder()
              .setConnectTimeout(Duration.ofSeconds(10))
              .setReadTimeout(Duration.ofSeconds(10))
              .build();
  </code></pre>
  
  <p>}</p>
  
  <p>Retrying getForEntity</p>
  
  <pre><code> public ResponseEntity getForEntity(URI uri, Class c) {
      return retryTemplate.execute(retryContext -&gt; {
          return restTemplate.getForEntity(uri, c);
      });
  }
  </code></pre>
  
  <p>Unit Test</p>
  
  <pre><code>public class RetryRestTemplateTest {
  
  @Mock
  private RestTemplate restTemplate;
  
  @Mock
  private RetryTemplate retryTemplate;
  
  private RetryRestTemplate retryRestTemplate;
  
  String testUrl = "http://localhost:8080";
  
  @Before
  public void setup() {
      MockitoAnnotations.initMocks(this);
  
      retryRestTemplate = new RetryRestTemplate(
              restTemplate,
              retryTemplate
      );
  }
  
  @Test
  public void getForEntity() throws URISyntaxException{
      URI testUri= new URI(testUrl);
      ArgumentCaptor&lt;URI&gt; argument = ArgumentCaptor.forClass(URI.class);
  
      doReturn(new ResponseEntity&lt;&gt;("ResponseString", HttpStatus.OK))
              .when(restTemplate).getForEntity(any(URI.class), eq(String.class));
  
      assertThat(restTemplate.getForEntity(testUri, String.class), is(HttpStatus.OK));
  
      verify(restTemplate).getForEntity(argument.capture(), eq(String.class));
      assertThat(argument.getValue().toString(), is(testUri));
  }}
  </code></pre>
  
  <p>My expected should be is &lt;200 OK> and my actual is &lt;&lt;200 OK OK,ResponseString,[]>></p>
  
  <p>Any help on this would be helpful as I am not that experienced with Mockito and Junit.</p>