简体   繁体   English

如何使用Mockito模拟和验证方法中的回调

[英]How to mock and verify a callback in method using Mockito

Within this method, I want to mock and ensure that mSharedPrefsManager gets called when I don't pass in a certain email string. 在此方法中,我想模拟并确保当我不传递某些电子邮件字符串时调用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
        }
    });
}

However, with my test I'm not able to catch whether mSharedPrefsManager is called. 但是,通过我的测试,我无法捕获是否mSharedPrefsManager Mockito says that .storePoints() is never called. Mockito说从未调用.storePoints() I thought about doing a doReturn().when() but as this is within the method that wouldn't work, would it? 我考虑过做一个doReturn().when()但是由于这在无法使用的方法中,会吗?

How do I catch the interactions on sharedPrefsManager ? 如何捕获sharedPrefsManager上的交互?

Mockito also says that .updateSilhouette() is not called. Mockito还说未调用.updateSilhouette() Do I need to mock onRetrieveCompleted() somehow? 我是否需要以某种方式模拟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));
    }
}

You are mocking: 你在嘲笑:

@Mock
RetrieveImpl retrieveInteractor;

This means that when you call retrieveInteractor.buildRetrieveRequest(..) , the real implementation is not invoked and eventually the methods that you expect to be called within that method call are never called.. 这意味着,当您调用retrieveInteractor.buildRetrieveRequest(..) ,不会调用真正的实现,并且最终永远不会调用您希望在该方法调用中调用的方法。

Try using @Spy instead, this will actually allow for the real implementation to be called and you can verify that object also: 尝试使用@Spy代替,这实际上将允许调用实际的实现,您还可以验证该对象:

@Spy
RetrieveImpl retrieveInteractor;

Just one the side.. in think you are testing too much there and going to deep in your verifications. 只是一个方面..以为您在那儿测试太多了,并且会深入进行验证。

That test in my opinion should be done for the RetrieveImpl.OnRetrieveCompletedListener class. 我认为该测试应该针对RetrieveImpl.OnRetrieveCompletedListener类进行。 Not the one that is in your question. 不在您的问题中。

But thats just to my taste.. 但这只是我的味道。

Attempting to use @Spy caused a lot of issues for me as RetrieveImpl interacts with a network. RetrieveImpl与网络交互时,尝试使用@Spy对我造成了很多问题。

I instead used a Captor and captured the callback. 我改用Captor捕获了回调。

@Captor
private ArgumentCaptor<RetrieveImpl.OnRetrieveCompletedListener> mOnRetrieveCompletedListenerCaptor;

...

@Test
public void isTest()
{
    ...
    verify(retrieveInteractor).buildRetrieveRequest(eq(email), mOnRetrieveCompletedListenerCaptor.capture());
    mOnRetrieveCompletedListenerCaptor.getValue().onRetrieveCompleted(mockMaitreBaseGsonSuccessful, retrieveInteractor);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM