How do I mock a REST template exchange?
<p>I have a service in which I need to ask an outside server via rest for some information:</p>

<pre><code>public class SomeService {

    public List&lt;ObjectA&gt; getListofObjectsA() {
        List&lt;ObjectA&gt; objectAList = new ArrayList&lt;ObjectA&gt;();
        ParameterizedTypeReference&lt;List&lt;ObjectA&gt;&gt; typeRef = new ParameterizedTypeReference&lt;List&lt;ObjectA&gt;&gt;() {};
        ResponseEntity&lt;List&lt;ObjectA&gt;&gt; responseEntity = restTemplate.exchange("/objects/get-objectA", HttpMethod.POST, new HttpEntity&lt;&gt;(ObjectAList), typeRef);
        return responseEntity.getBody();
    }
}
</code></pre>

<p>How can I write a JUnit test for <code>getListofObjectsA()</code>?</p>

<p>I have tried with the below:</p>

<pre><code>@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {
    private MockRestServiceServer mockServer;

    @Mock
    private RestTemplate restTemplate;

    @Inject
   private SomeService underTest;

@Before
public void setup() {
    mockServer = MockRestServiceServer.createServer(restTemplate);
    underTest = new SomeService(restTemplate);
    mockServer.expect(requestTo("/objects/get-objectA")).andExpect(method(HttpMethod.POST))
            .andRespond(withSuccess("{json list response}", MediaType.APPLICATION_JSON));
}

    @Test
    public void testGetObjectAList() {
    List&lt;ObjectA&gt; res = underTest.getListofObjectsA();
    Assert.assertEquals(myobjectA, res.get(0));
}
</code></pre>

<p>However the above code does not work, it shows that <code>responseEntitty</code> is <code>null</code>.  How can I correct my test to properly mock <code>restTemplate.exchange</code>?</p>