在这种方法中,我希望模拟并确保在我不传入某个特定的电子邮件字符串时调用
然而,通过我的测试,我无法捕捉是否调用了“mSharedPrefsManager”。Mockito表示从未调用“.storePoints()”。我考虑使用“doReturn().when()”,但由于它在方法内部,所以不起作用,是吗?
我该如何捕捉对“sharedPrefsManager”的交互?
Mockito还表示未调用“.updateSilhouette()”。我需要以某种方式模拟“onRetrieveCompleted()”吗?
mSharedPrefsManager
。@Override
public void retrieveWithEmail(final String email, final WelcomeContract.Presenter presenter)
{
retrieveInteractor.buildRetrieveRequest(email, new RetrieveImpl.OnRetrieveCompletedListener()
{
@Override
public void onRetrieveCompleted(final MaitreBaseGson retrieveResponse, RetrieveImpl retrieveClass)
{
if (retrieveResponse.getStatus().equals(mContext.getString(R.string.ok)))
{
if (!email.equals("certain@email.com"))
mSharedPrefsManager.storePoints(Integer.parseInt(retrieveResponse.getData().getPoints()));
presenter.updateSilhouette(retrieveResponse);
}
// Silently swallow failures
}
});
}
然而,通过我的测试,我无法捕捉是否调用了“mSharedPrefsManager”。Mockito表示从未调用“.storePoints()”。我考虑使用“doReturn().when()”,但由于它在方法内部,所以不起作用,是吗?
我该如何捕捉对“sharedPrefsManager”的交互?
Mockito还表示未调用“.updateSilhouette()”。我需要以某种方式模拟“onRetrieveCompleted()”吗?
@RunWith(MockitoJUnitRunner.class)
public class WelcomeInteractorTest
{
@Mock
RetrieveImpl retrieveInteractor;
@Mock
WelcomePresenter welcomePresenter;
@Mock
SharedPrefsManager sharedPrefsManager;
@Mock
Context context;
@InjectMocks WelcomeInteractorImpl welcomeInteractor;
@Mock
RetrieveImpl.OnRetrieveCompletedListener onRetrieveCompletedListener;
@Test
public void RetrieveWithCertainEmail_SavePoints()
{
welcomeInteractor.retrieveWithEmail("certain@email.com", welcomePresenter);
verify(retrieveInteractor).buildRetrieveRequest(eq("certain@email.com"), any(RetrieveImpl.OnRetrieveCompletedListener.class));
verify(sharedPrefsManager).storePoints(any(Integer.class));
verify(welcomePresenter).updateSilhouette(any(MaitreBaseGson.class));
}
}