Мой код класса обслуживания указан ниже:
public class MyServiceImpl implements MegatillAccessService {
@Autowired
RestTemplate restTemplate;
@Value("${api.key}")
private String apiKey;
@Value("${customers.url}")
private String postUrl;
@Override
public String pushCustomerData(List<Customer> listOfcustomers, String storeId) throws MyServiceException {
Set<Customer> setOfCustomers = new HashSet<>(listOfcustomers);
int noOfCustomersLoadedSuccessfully =0;
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("apiKey", apiKey);
headers.add("Content-Type", "application/json");
headers.add("storeId", storeId);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
for(Customer customer: setOfCustomers){
HttpEntity<Customer> request = new HttpEntity<Customer>(customer, headers);
CustomerDataDto customerDataDto = null;
try {
customerDataDto = restTemplate.exchange(postUrl, HttpMethod.POST, request, CustomerDataDto.class).getBody();
}
catch (HttpClientErrorException ex) {
if (ex.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
log.error("The customers service is not available to load data: "+ ex.getResponseBodyAsString(), ex);
throw new MyServiceException("The customers service is not available to load data",new RuntimeException(ex));
}
else{
log.warn("Error for customer with alias: "+customer.getAlias() +" with message: "+ ex.getResponseBodyAsString(), ex);
if(!ex.getResponseBodyAsString().contains("already found for this shop")){
throw new MyServiceException("An error occurred while calling the customers service with status code "+ex.getStatusCode(),new RuntimeException(ex));
}
}
}
catch(Exception e){
throw new MyServiceException("An error occurred while calling the customers service: ",new RuntimeException(e));
}
if(null != customerDataDto) {
noOfCustomersLoadedSuccessfully++;
log.debug("--------Data posted successfully for: ---------"+customerDataDto.getAlias());
}
}
String messageToReturn = "No. of unique customers from source: "+setOfCustomers.size()+". No. of customers loaded to destination without error: "+noOfCustomersLoadedSuccessfully;
return messageToReturn;
}
}
Мой тестовый класс, как показано ниже:
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
public class MyServiceTest {
@InjectMocks
private MyService myService = new MyServiceImpl();
@Mock
RestTemplate restTemplate;
@Before
public void setUp() throws Exception
{
MockitoAnnotations.initMocks(this);
initliaizeModel();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
}
@Test
public void pushAllRecords(){
Mockito.when(restTemplate.exchange(Matchers.anyString(), Matchers.any(HttpMethod.class), Matchers.<HttpEntity<?>> any(), Matchers.<Class<CustomerDataDto>> any()).getBody()).thenReturn(customerDataDto);
/*Mockito.when(restTemplate.exchange(Mockito.anyString(),
Mockito.<HttpMethod> eq(HttpMethod.POST),
Matchers.<HttpEntity<?>> any(),
Mockito.<Class<CustomerDataDto>> any()).getBody()).thenReturn(customerDataDto);*/
String resultReturned = myService.pushCustomerData(customers,"1235");
assertEquals(resultReturned, "No. of unique customers from source: 2. No. of customers loaded to destination without error: 2");
}
}
Во время выполнения теста я получаю исключение NullPointerException в строке, где я даю условие Mockito.when и thenReturn. Я перепробовал много комбинаций, но он все еще дает NPE. Я даже не могу добраться до вызова метода. Можете ли вы дать мне знать, где я ошибаюсь?