The function to be mocked is of class ContactsProductionService.java
@Override
public ContactsProductionDto getContactsProduction(long id) {
Site site = siteDao.find(id);
ContactsProductionDto contactsProductionDto = new ContactsProductionDto();
List<Entite> entities = entiteDao.findEntitesByType(Type.CP);
for (Entite entite : entities) {
ProductionCenterDto productionCenter = new ProductionCenterDto();
productionCenter.setId(entite.getId());
productionCenter.setCode(entite.getCode());
productionCenter.setNumber(entite.getNumero());
productionCenter.setName(entite.getDescription());
contactsProductionDto.getProductionCenters().add(productionCenter);
}return contactsProductionDto;
And this is the test:
@Test
public void testGetContactsProduction(){
Mockito.when(siteDao.find(id)).thenReturn(new Site());
List<Entite> entities = null;
Mockito.when(entiteDao.findEntitesByType(Type.CP)).thenReturn(entities);
ContactsProductionDto contact=contactsProductionService.getContactsProduction(id);
Assert.assertNotNull(contact);
}
I'm getting NullPointerException on running this test. The cause is that inside the for loop value of Entite is null!! Any help please :(