Это метод Java в этой строке:
ResponseEntity<Response> response =
restTemplate.exchange(achAccountDetailsURL, HttpMethod.GET, requestEntity, Response.class);
Я получаю NullPointerException
при насмешке в классе JUnit
.
public static Map<String, BankDetailsDTO> getACHAccountsDataWithBankName() {
achResponseDetails = Collections.emptyMap();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<?> requestEntity = new HttpEntity<>(headers);
ResponseEntity<Response> response =
restTemplate.exchange(achAccountDetailsURL, HttpMethod.GET,
requestEntity, Response.class);
if (response.getStatusCode().equals(HttpStatus.OK)) {
ObjectMapper mapper = new ObjectMapper();
return mapper.convertValue(response.getBody().getData(),
new TypeReference<Map<String, BankDetailsDTO>>() {}); //enter code here
}
else {
throw new CustomException("Unable to fetch user privileges from UserMS");
}
}
Это класс JUnit
, и я устанавливаю RestTemplate
в приведенном ниже коде (это правильно?):
@Test(expected = NullPointerException.class)
public void getFilteredResolvedAchExceptionsTest() throws Exception {
TypedQuery query = Mockito.mock(TypedQuery.class);
when(entityManager.createQuery(anyString(), eq(ACHExceptionEntity.class)))
.thenReturn(query);
when(query.setMaxResults(anyInt())).thenReturn(query);
when(query.getResultList()).thenReturn(achExceptionEntity);
Response testresponse = new Response();
ResponseEntity<Response> respEntity =
new ResponseEntity<Response>(testresponse, HttpStatus.ACCEPTED);
Mockito.when(this.restTemplate.exchange(Matchers.anyString(),
Matchers.any(HttpMethod.class),
Matchers.<HttpEntity<?>> any(),
Matchers.<Class<Response>> any()))
.thenReturn(respEntity); //enter code here
List<ACHExceptionDTO> achException =
achExpDAOImpl.getFilteredResolvedAchExceptions(requestParams);
assertEquals(achExceptionDTO, achException);
}