" Mockito 0匹配预期,3记录" - 为什么期望0匹配器?

时间:2017-10-04 19:08:39

标签: android mockito matcher

以前曾问过这个问题,但现有的答案并不适用于我的情况。

我想测试submitCode()方法:

public class VerificationCodeViewModel{

    //Input
    public final ObservableField<String> verificationCode           = new ObservableField<>();

    //Output
    public final ObservableField<String> requestError               = new ObservableField<>();
    public final ObservableBoolean loading                          = new ObservableBoolean();
    public final ObservableField<LoginCredentials> loginCredentials = new ObservableField<>();

    @NonNull private final Context context;
    @NonNull private final UnverifiedUser unverifiedUser;
    @NonNull private final CampaignRepository campaignRepository;
    @NonNull private final AccountRepository accountRepository;
    @NonNull private final VerificationCodeNavigator navigator;

    public VerificationCodeViewModel(@NonNull Context context,
                                     @NonNull UnverifiedUser unverifiedUser,
                                     @NonNull CampaignRepository campaignRepository,
                                     @NonNull AccountRepository accountRepository,
                                     @NonNull VerificationCodeNavigator navigator) {

        this.context = context;
        this.unverifiedUser = unverifiedUser;

        this.campaignRepository = campaignRepository;
        this.accountRepository = accountRepository;
        this.navigator = navigator;
    }

    public void submitCode() {

        loading.set(true);

        String sourceCampaign = null;
        if (campaignRepository.getCampaign() != null) {
            sourceCampaign = campaignRepository.getCampaign().getSource();
        }

        this.accountRepository.verifyMobileNumber(
                this.unverifiedUser,
                this.verificationCode.get(),
                sourceCampaign,
                new AccountDataSource.VerifyMobileNumberCallback() {
                    @Override
                    public void onVerificationSuccess(UnverifiedUser.Entity entity) {
                        loading.set(false);
                        loginCredentials.set(createLoginCredentials());
                        navigator.finishActivity(true);
                    }

                    @Override
                    public void onVerificationFailure(@Nullable String message) {
                        loading.set(false);
                        requestError.set(message);
                    }
                }
        );
    }
}

我有以下测试用例:

public class VerificationCodeViewModelTests {

    private VerificationCodeViewModel viewModel;

    @Mock private Context context;

    @Mock private UnverifiedUser unverifiedUser;

    @Mock private CampaignRepository campaignRepository;

    @Mock private AccountRepository accountRepository;

    @Mock private VerificationCodeNavigator navigator;

    @Mock private ArgumentCaptor<AccountDataSource.VerifyMobileNumberCallback> verifyMobileNumberCallbackCaptor;


    @Before
    public void setupVerificationCodeViewModel(){

        MockitoAnnotations.initMocks(this);

        viewModel = new VerificationCodeViewModel(
                context,
                unverifiedUser,
                campaignRepository,
                accountRepository,
                mock(VerifyMobileNumberActivity.class)//navigator
        );
    }

    @Test
    public void testSubmitCode(){

        viewModel.verificationCode.set(VERIFICATION_CODE);
        viewModel.submitCode();

        assertTrue(viewModel.loading.get());

        verify(accountRepository).verifyMobileNumber(
                eq(unverifiedUser),//line 132
                eq(VERIFICATION_CODE),//line 133
                eq(CAMPAIGN_SOURCE),//line 134
                verifyMobileNumberCallbackCaptor.capture());//line 135

        UnverifiedUser.Entity entity = mock(UnverifiedUser.Entity.class);
        when(entity.getId()).thenReturn(ENTITY_ID);

        verifyMobileNumberCallbackCaptor.getValue().onVerificationSuccess(entity);

        assertFalse(viewModel.loading.get());
        assertEquals(viewModel.loginCredentials.get().getUsername(),UNVERIFIED_USER_EMAIL);
        assertEquals(viewModel.loginCredentials.get().getPassword(),UNVERIFIED_USER_PASSWORD);

        verify(navigator).finishActivity(true);
    }
}

当我验证调用了accountRepository.verifyMobileNumber时,我收到以下错误:

  

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:   参数匹配器的使用无效! 0匹配预期,3记录:    - &GT; at ... testSubmitCode(VerificationCodeViewModelTests.java:132)    - &GT; at ... testSubmitCode(VerificationCodeViewModelTests.java:133)    - &GT;在... testSubmitCode(VerificationCodeViewModelTests.java:134)

     

如果匹配器与原始值组合,则可能会发生此异常:       //不正确:       someMethod(anyObject(),&#34; raw String&#34;);使用匹配器时,所有参数都必须由匹配器提供。例如:       //正确:       someMethod(anyObject(),eq(&#34; matcher by matcher&#34;));

     

有关更多信息,请参阅Matchers类的javadoc。

     

在   ... VerificationCodeViewModelTests.testSubmitCode(VerificationCodeViewModelTests.java:135)

我不明白为什么会说预期0场比赛?其他答案建议将eq(..)替换为any(...)isA(..)。 首先,我不相信这是适用的,因为错误是首先没有预期匹配者; 其次,我已经尝试过,问题仍然存在。

如果有人能够解释为什么会出现0匹配,以及如何解决这个问题,我将不胜感激。

更新

AccountRepository.verifyMobileNumber()的实施是:

AccountRepository.java

public class AccountRepository implements AccountDataSource {
    @Override
    public void verifyMobileNumber(@NonNull UnverifiedUser unverifiedUser, 
                                   @NonNull String verificationCode, 
                                   @Nullable String sourceCampaign, 
                                   @NonNull VerifyMobileNumberCallback callback) {
        this.remoteSource.verifyMobileNumber(unverifiedUser, verificationCode, sourceCampaign, callback);
    }
}

AccountRemoteDataSource.java

public class AccountRemoteDataSource implements AccountDataSource {

    @Override
    public void verifyMobileNumber(@NonNull UnverifiedUser unverifiedUser,
                                   @NonNull String verificationCode,
                                   @Nullable String sourceCampaign,
                                   @NonNull final VerifyMobileNumberCallback callback) {

        accountService().verifyMobileNumber(/*params*/).enqueue(new Callback() {
            @Override
            public void onResponse(Response response, Retrofit retrofit) {
                try{
                    //parse response
                    callback.onVerificationSuccess(entity);
                } catch (Exception e) {
                    callback.onVerificationFailure(e.getMessage());
                }
            }

            @Override
            public void onFailure(Throwable t) {
                callback.onVerificationFailure(t.getMessage());
            }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

啊哈哈哈,找到了!您在测试文件的第六个带注释的字段中错误地使用了@Mock ArgumentCaptor

@Mock private ArgumentCaptor<AccountDataSource.VerifyMobileNumberCallback>
    verifyMobileNumberCallbackCaptor;

Mockito并没有专门设置自己的基础设施,所以它没有发现你试图模仿Mockito本身的事实。通过在ArgumentCaptor.capture()来电中调用方法verify,Mockito会假设您实际上是在尝试验证对capture的调用。

尽管语法巧妙,但Mockito实际上只是一个状态机,对verify(...)的调用启动验证,每次调用匹配器都会推送匹配器描述onto an internal stack,然后调用Mockito模拟会触发验证。 Mockito在参数匹配器堆栈上看到三个匹配器,用于对capture的零arg调用。这就是为什么记录了3个匹配器,预期为0的原因。

将该注释切换为@Captor,你应该好好去。