Mocking RestTemplate.exchange() is not working. The Response of restTemplate.exchange() mocking gives null value at BDS Adapter class. My test case is failing with null pointer exception in BDSAdapter class. (response.getStatusCodeValue() gives null pointer exception..Mockito hints 

Unused... -> at com..policydetails\_adapters.BDSAdapterTest.getInsuranceHoldings(BDSAdapterTest.java:56)
 [MockitoHint] ...args ok? -> at com.policydetails\_adapters.BDSAdapter.fetchInsuranceDetails(BDSAdapter.java:77)

```
 Below are my classes.

```

Test Class:
-----------

```
@RunWith(MockitoJUnitRunner.class)
public class BDSAdapterTest {

    @InjectMocks
    private BDSAdapter bdsAdapter;

    @Mock
    private BDSFetchInsuranceDetailsRequest bdsFetchInsuranceDetailsRequest;

    @Mock
    private RestTemplate restTemplate;

    @Mock
    Environment env;

    @Test
    public void getInsuranceHoldings() throws InsuranceHoldingsException {
        Mockito.when(restTemplate.exchange(
                ArgumentMatchers.anyString(),
                ArgumentMatchers.any(HttpMethod.class),
                ArgumentMatchers.any(),
                ArgumentMatchers.<Class<BDSFetchInsuranceDetailsResponse>>any()))
                             .thenReturn(sampleBDSCustomerInsuranceHoldings());
        BDSFetchInsuranceDetailsResponse bdsFetchInsuranceDetailsResponse = bdsAdapter.fetchInsuranceDetails("MBSG", "S6564318I", "1234", "007");
        assertNotNull("response is not null", bdsFetchInsuranceDetailsResponse);
    }

    public static ResponseEntity<BDSFetchInsuranceDetailsResponse> sampleBDSCustomerInsuranceHoldings() {
        BDSFetchInsuranceDetailsResponse bdsResponse = new BDSFetchInsuranceDetailsResponse();
        Header header = new Header();
        header.setChannelId("MBSG");
        header.setMsgId("4aBE50ZrQtjVuXfTyALJ");
        bdsResponse.setHeader(header);
        ResponseEntity<BDSFetchInsuranceDetailsResponse> response = new ResponseEntity<BDSFetchInsuranceDetailsResponse>(bdsResponse, HttpStatus.ACCEPTED);
        return response;
    }

}

```

My Actual class
---------------

@Component
public class BDSAdapter {

```
@Autowired
RestTemplate restTemplate;

@Autowired
BDSFetchInsuranceDetailsRequest bdsFetchInsuranceDetailsRequest;

@Autowired
Environment env;

public BDSFetchInsuranceDetailsResponse fetchInsuranceDetails(String channelId, String customerId,
        String insurerCode, String policyNumber) throws InsuranceHoldingsException {
    BDSFetchInsuranceDetailsResponse bdsFetchInsuranceDetailsResponse = null;
    try {
        logger.info("Inside BDSAdapter::fetchInsuranceDetails");
        Header header = new Header();
        header.setMsgId(RandomStringUtils.randomAlphanumeric(20));
        header.setChannelId(channelId);
        bdsFetchInsuranceDetailsRequest.setHeader(header);

        HttpHeaders requestHeaders = new HttpHeaders();

        requestHeaders.add("Accept", MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<BDSFetchInsuranceDetailsRequest> requestEntity = new HttpEntity<>(bdsFetchInsuranceDetailsRequest, requestHeaders);

        ResponseEntity<BDSFetchInsuranceDetailsResponse> response = restTemplate.exchange(env.getProperty("bds_fetch_insurance_details_url"),HttpMethod.POST, requestEntity, BDSFetchInsuranceDetailsResponse.class);

        if(response.getStatusCodeValue() == 204) {
            throw new InsuranceHoldingsException(response.getStatusCode().toString(), "No Content");
        }
        bdsFetchInsuranceDetailsResponse = response.getBody();

    } catch (Exception e) {
        e.printStackTrace();
        }
    }
    return bdsFetchInsuranceDetailsResponse;
}

```

}