No matter what I've tried, test end up on NullPointerException on stubbing statement. Method to test:
public boolean selectEntity(final List<T> entities) {
if (entities.contains(helper.getLastEntity())) {
return true;
}
}
Above snippet is enough, cause (in unit test) cannot even enter into conditional statement. To clarify: getLastEntity returns field
private T lastEntity
for object of class Helper. T is an interface.
My best try is:
private @Mock Helper helper;
private @Mock List<T> entities;
...
@Test
public void testSelectEntity(){
when(entities.contains(notNull(T.class))).thenReturn(true);
when(helper.getLastEntity()).thenReturn((T) anyObject());
}
How to proceed here?
UPDATE: follow your suggestions, I rewrote test (mocks are for sure initialized this time:))
final DummyT dummyT = new DummyT();
when(helper.getLastEntity()).thenReturn(dummyT);
when(entities.contains(dummyT).thenReturn(true);
assertTrue(objectUnderTest.selectEntity(entities));
where DummyT implements T. Got null pointer on method execution, pointing on if statement.