Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito - 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 NullPointerException when i run my junit test in iEmployeeService mocking. There are not Exception 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);
}

My JUnit test is annotated with @RunWith(MockitoJUnitRunner.class)

@Mock
ISecurityLoginService iSecurityLoginService;

@Mock
IEmployeeService iEmployeeService;

@InjectMocks
ServiceCalendarViewBean serviceCalendarViewBean  = new ServiceCalendarViewBean();

@Before public void initMocks() {
           MockitoAnnotations.initMocks(this);
}

@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();
}
like image 968
Eva Avatar asked Mar 05 '15 12:03

Eva


People also ask

How do you mock and inject a mock?

Mockito @InjectMocks annotations allow us to inject mocked dependencies in the annotated class mocked object. This is useful when we have external dependencies in the class we want to mock. We can specify the mock objects to be injected using @Mock or @Spy annotations.

What is the difference between mock and inject mock?

@Mock is used to create mocks that are needed to support the testing of the class to be tested. @InjectMocks is used to create class instances that need to be tested in the test class. Annotated class to be tested dependencies with @Mock annotation.

Can we use @qualifier with @mock?

@MockBean with @QualifierIf an interface has more than one implementation class then to choose the exact class for dependency injection, @Qualifier annotation is used. Here we will show the demo to use @MockBean with @Qualifier annotation. Find the classes used in our unit test class.

Is MockitoAnnotations initMocks deprecated?

initMocks. Deprecated. Use openMocks(Object) instead. This method is equivalent to openMocks(testClass).


1 Answers

Not related to the question, but useful to know !

If the test is annotated with @RunWith(MockitoJUnitRunner.class) then MockitoAnnotations.initMocks(this); is not necessary (it may even cause issues when injecting), the mockito runner performs injection and additional stuffs to validate mocks.

Also having both mock init mechanisms may cause trouble with injection and stubbing, this is due to the way the lifecyle of JUnit test and how mockito unit-integration code is used :

  1. The runner will create mocks and inject those mocks in the test object.
  2. Then the @Before methods kicks in and recreate new mocks, and may not perform injection as the object is already initialized.
like image 185
Brice Avatar answered Oct 20 '22 00:10

Brice