I'm using Android SDK and junit4 + Mockito for unit testing. Say I have a class like this in my app:
public class Container{
@NonNull private Set<String> values = new HashSet<>();
public void addValue(String value) {
values.add(value);
}
@NonNull
public Set<String> getValues() {
return values;
}
}
And I also have a unit test with Mockito that looks like this:
public class ContainerTest {
private Container container;
@Before
public void before() {
container = mock(Container.class);
}
@Test
public void shouldAddValue() {
container.add("test_value");
assertTrue(container.getValues.contains("test_value"));
}
}
This test actually fails on line "container.add("test_value");" because mock(Container.class) creates a class in which values field is actually set to null, so values.add(value) in addValue() method throws an NPE. I could add a null check in addValue() to fix this, but that seems absurd, since values are already declared non null.
Is there any way to make Mockito respect @NonNull annotations and initialize the field properly?