jeudi 5 mars 2015

Mock not being injected for one of the testcases

I have a jsf spring application and using mockito for my unit test.I keep getting null pointer exception when i run my unit test iEmployeeService. There is no null pointer for iSecurityLoginService.


Method to be mocked



@Autowired
IEmployeeService iEmployeeService;
@Autowired
ISecurityLoginService iSecurityLoginService;
public void addEvent() {

entityEventsCreate.setTitle(entityEventsCreate.getTitle());

entityEventsCreate.setModifiedBy(iSecurityLoginService
.findLoggedInUserId());

int eventId = iEmployeeService
.addEmployeeTimeOff(entityEventsCreate);
}


Unit test is



@Mock
ISecurityLoginService iSecurityLoginService;

@Mock
IEmployeeService iEmployeeService;

@InjectMocks
ServiceCalendarViewBean serviceCalendarViewBean = new ServiceCalendarViewBean();
@Test
public void testSaveEvent() {
Mockito.when(iSecurityLoginService.findLoggedInUserId()).thenReturn(1);

serviceCalendarViewBean.getEntityEventsCreate().setTitle("Junit Event Testing");

Mockito.when(iSecurityLoginService.findLoggedInUserId()).thenReturn(1);
Mockito.when(iEmployeeService.addEmployeeTimeOff(Mockito.any(Events.class))).thenReturn(2);

serviceCalendarViewBean.addEvent();
}

Aucun commentaire:

Enregistrer un commentaire