title: How do I mock Rest Template's getForEntity() method
body: <p>I am new to mockito and need some help, probably some sample on how to mock Rest Template's getForEntity and postForEntity methods. Below is the code for which I want to write Junit test cases by mocking the getForEntity mehod. </p>
  
  <p>SomeService.java</p>
  
  <pre><code>class SomeService
  {
     //some private, static,  final data members
     public Map getService(String sNo, String uId, String en)
     {
        ResponseEntity &lt;Map&gt; response = new 
        RestTemplate().getForEntity("https://someurl.com/someService", 
        Map.class);
        Map body = response.getBody();
        //do something
  
        HttpEntity&lt;?&gt; request = new HttpEntity &lt;&gt;(payLoad, headers);
        //payload is Hash Map and headers is a LinkedMultiValueMap
        ResponseEntity &lt;Map&gt; response = new RestTemplate().postForEntity(url, 
        request, headers);
        return response.getBody(); 
  
     }   
  }
  </code></pre>
  
  <p>I have tried doing something with @Mock and @InjectMocks. </p>
  
  <p>TestSomeService.java</p>
  
  <pre><code>@RunWith(MockitoJunitRunner.class)
  class TestSomeService
  {
     @Mock
     RestTemplate restTemplate;
     @InjectMocks
     SomeService ser;
     /*Some data members*/
     @Before
     {
        HttpEntity &lt;?&gt; request = new HttpEntity&lt;&gt;(reqPayload, headers);
        Mockito.when(restTemplate.getForEntity("theUrl", 
        Map.class)).thenReturn(new ResponseEntity &lt;Map&gt;(someMap, 
        HttpStatus.OK));
        Mockito.when(restTemplate.postForEntity("anotherUrl", request, 
        Map.class)).thenReturn(new ResponseEntity &lt;Map&gt;(expectedMap, 
        HttpStatus.OK));
  
     }  
     @Test
     public void testGetService()
     {
         Map &lt;String, Object&gt; result = ser.getService("123", "abc", "QA");
     }    
  }
  </code></pre>