I'm trying to test an abstract class and Mockito does not initialize my member variables. Here's a simple example to show you my problem.
This is an abstract class that initializes its 'field' member:
import java.util.ArrayList;
import java.util.Collection;
public abstract class Foo {
private final Collection field = new ArrayList();
protected Foo() {
System.out.println("In constructor");
}
public boolean isNull(Object o) {
field.add(o);
return o == null;
}
abstract void someAbstractMethod();
}
Here the test class:
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
public class FooTest {
@Test
public void testSomething() {
final Foo foo = Mockito.mock(Foo.class);
Mockito.when(foo.isNull(Mockito.anyObject())).thenCallRealMethod();
Assert.assertFalse(foo.isNull("baaba"));
}
}
When the test is run it throws a NPE because the variable 'field' is not initialized!
What am I doing wrong?