Mockito - Mock not being injected for one of the testcases
Solution 1
I solved the problem..In my spring bean, i had 2 objects for the same service interface. so the mock was being set for the first interface object.
Ex: in my bean i had,
@Autowired
IEmployeeService employeeService;
@Autowired
IEmployeeService iEmployeeService;
So the mock create for the IEmployeeservice interfaces was being inject for the first service object irrelevant for their names.
@Mock
IEmployeeService iEmployeeService;
ie, the mock object 'iEmployeeService' was injected to beans 'employeeService' .
Thank you for all those who helped..:)
Solution 2
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 :
- The runner will create mocks and inject those mocks in the test object.
- Then the
@Before
methods kicks in and recreate new mocks, and may not perform injection as the object is already initialized.
Solution 3
Try to add this
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
Eva
Updated on June 28, 2022Comments
-
Eva almost 2 years
I have a jsf spring application and using mockito for my unit test. I keep getting
NullPointerException
when i run myjunit
test iniEmployeeService
mocking. There are notException
foriSecurityLoginService
.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(); }
-
Bono about 9 yearsThis does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
-
Bjørn-Roger Kringsjå about 9 years@Bono I recommend you read this MSE post: Your answer is in another castle: when is an answer not an answer?
-
skw about 7 yearsSo this is the problem you had, but could you kindly post how you solved your problem, please? EDIT: I found an answer here: stackoverflow.com/questions/21054057/…
-
ihebiheb about 5 yearsIt's maybe not related, but this was the cause of the error in my case (it seemed like mockito was ignoring the Mockito.when(xxx).thenReturn(yyy)). Deleting MockitoAnnotations.initMocks(this); solved it. Thank you for sharing.