我正在使用Mockito做JUnit測試,並且發現了一個斷言問題。我創建一個模擬對象,然後使用模擬對象創建一個演示者對象。斷言Mockito嘲笑的對象被設置爲空
@Mock
Object mFooBar;
private FooPresenter mFooPresenter;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mFooPresenter = new FooPresenter(mFooBar);
}
在我的演示者的onDestroy()方法中,我將對象清空。
public FooPresenter(Object fooBar) {
mFooBar = fooBar;
}
@Override
public void onDestroy() {
mFooBar = null;
}
然後,當我嘗試在我的FooPresenterTest中爲mFooBar聲明NULL時,它失敗,因爲它不爲空。
@Test
public void testThatObjectsAreNullifiedInOnDestroy() throws Exception {
fooPresenter.onDestroy();
assertNull(mFooBar);
}
這未按
Expected :<null>
Actual :mFooBar
我的問題,然後是,如何在我的測試類來處理的嘲笑對象的引用相比,我實例化運行測試的對象?爲什麼assertNull在應該被設置爲null時失敗?
嘿@ kerseyd27,我假設你的'FooPresenter'中的'mFooBar'字段是私人的,是否有任何財產暴露它? – abest
嘿@abest是的它是私人的,並沒有暴露的屬性。 – kerseyd27