"Mockito 0 matchers expected, 3 recorded" - 为什么需要 0 个匹配器?
"Mockito 0 matchers expected, 3 recorded" - Why are 0 matchers expected?
之前有人问过这个问题,但现有的答案并不完全适用于我的情况。
我想测试 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:
Invalid use of argument matchers! 0 matchers expected, 3 recorded:
-> at ...testSubmitCode(VerificationCodeViewModelTests.java:132)
-> at ...testSubmitCode(VerificationCodeViewModelTests.java:133)
-> at ...testSubmitCode(VerificationCodeViewModelTests.java:134)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
at
...VerificationCodeViewModelTests.testSubmitCode(VerificationCodeViewModelTests.java:135)
我不明白的是为什么它说 0 matches expected?其他答案建议将 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());
}
});
}
}
哈哈哈哈,找到了!您在测试文件的第六个注释字段中错误地使用了 @Mock ArgumentCaptor
。
@Mock private ArgumentCaptor<AccountDataSource.VerifyMobileNumberCallback>
verifyMobileNumberCallbackCaptor;
Mockito 没有对自己的基础结构进行特殊处理,因此它没有发现您正在尝试模拟 Mockito 本身的事实。通过在 verify
调用中间调用方法 ArgumentCaptor.capture()
,Mockito 假设您实际上是在尝试验证对 capture
.
的调用
尽管语法巧妙,Mockito 实际上只是一个状态机,其中对 verify(...)
的调用开始验证,每次调用匹配器都会推送一个匹配器描述 onto an internal stack,然后是下一个调用 Mockito 模拟会触发验证。 Mockito 在对 capture
的零参数调用的参数匹配器堆栈上看到三个匹配器。这就是为什么记录了 3 个匹配器,而预期为 0 个。
将该注释切换为 @Captor
,您就可以开始了。
之前有人问过这个问题,但现有的答案并不完全适用于我的情况。
我想测试 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: Invalid use of argument matchers! 0 matchers expected, 3 recorded: -> at ...testSubmitCode(VerificationCodeViewModelTests.java:132) -> at ...testSubmitCode(VerificationCodeViewModelTests.java:133) -> at ...testSubmitCode(VerificationCodeViewModelTests.java:134)
This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example: //correct: someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
at ...VerificationCodeViewModelTests.testSubmitCode(VerificationCodeViewModelTests.java:135)
我不明白的是为什么它说 0 matches expected?其他答案建议将 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());
}
});
}
}
哈哈哈哈,找到了!您在测试文件的第六个注释字段中错误地使用了 @Mock ArgumentCaptor
。
@Mock private ArgumentCaptor<AccountDataSource.VerifyMobileNumberCallback>
verifyMobileNumberCallbackCaptor;
Mockito 没有对自己的基础结构进行特殊处理,因此它没有发现您正在尝试模拟 Mockito 本身的事实。通过在 verify
调用中间调用方法 ArgumentCaptor.capture()
,Mockito 假设您实际上是在尝试验证对 capture
.
尽管语法巧妙,Mockito 实际上只是一个状态机,其中对 verify(...)
的调用开始验证,每次调用匹配器都会推送一个匹配器描述 onto an internal stack,然后是下一个调用 Mockito 模拟会触发验证。 Mockito 在对 capture
的零参数调用的参数匹配器堆栈上看到三个匹配器。这就是为什么记录了 3 个匹配器,而预期为 0 个。
将该注释切换为 @Captor
,您就可以开始了。