Mocking RestTemplate.exchange () не работает. Ответ mocking restTemplate.exchange () дает нулевое значение в классе адаптера BDS. Мой тестовый пример не работает с исключением нулевого указателя в классе BDSAdapter. (response.getStatusCodeValue () дает исключение с нулевым указателем .. Подсказки Mockito
Unused ... -> в com..policydetails_adapters.BDSAdapterTest.getInsuranceHoldings (BDSAdapterTest. java: 56) [MockitoHint] ... args ok? -> at com.policydetails_adapters.BDSAdapter.fetchInsuranceDetails (BDSAdapter. java: 77)
Below are my classes.
Тестовый класс:
@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;
}
}
Мой фактический класс
@ Компонент publi c класс 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;
}
}