I am using Mockito to do JUnit tests, and stuck figuring out an assertion issue. I'm creating a mock object, and then creating a presenter object using the mocked object.
@Mock
Object mFooBar;
private FooPresenter mFooPresenter;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mFooPresenter = new FooPresenter(mFooBar);
}
In the onDestroy() method in my presenter, I null out the object.
public FooPresenter(Object fooBar) {
mFooBar = fooBar;
}
@Override
public void onDestroy() {
mFooBar = null;
}
Then when I try to assertNull for mFooBar in my FooPresenterTest, it fails because it is not null.
@Test
public void testThatObjectsAreNullifiedInOnDestroy() throws Exception {
fooPresenter.onDestroy();
assertNull(mFooBar);
}
This fails as
Expected :<null>
Actual :mFooBar
My questions then are, how are the references handled to the mocked objects in my test class compared to the object I instantiated to run the test? Why does assertNull fail when it should have been set to null?