title: How to mock RestTemplate in Java Spring?
body: <pre><code>public class ServiceTest {
      @Mock
      RestTemplate restTemplate = new RestTemplate();
      @InjectMocks
      Service service = new Service();
      ResponseEntity responseEntity = mock(ResponseEntity.class);
  
      @Test
      public void test() throws Exception {
          Mockito.when(restTemplate.getForEntity(
                  Mockito.anyString(),
                  Matchers.any(Class.class)
                  ))
                  .thenReturn(responseEntity);
          boolean res = service.isEnabled("something");
          Assert.assertEquals(res, false);
      }
  </code></pre>
  
  <p>I tried to test a simple test for a service including a restclient. It looks I have not Mock the <code>RestTemplate</code> successfully. It looks like the code get the real data not the mock one. Anyone can help me with this.</p>
  
  <p>The service itself will looks as this:</p>
  
  <pre><code>public class Service{
      public boolean isEnabled(String xxx) {
          RestTemplate restTemplate = new RestTemplate();
          ResponseEntity&lt;String&gt; response = restTemplate.getForEntity("someurl",String.class);
          if(...)return true;
          return false;
      }
  }
  </code></pre>