gpt4 book ai didi

android - "Mockito 0 matchers expected, 3 recorded"- 为什么需要 0 个匹配器?

转载 作者:行者123 更新时间:2023-11-29 19:07:45 24 4
gpt4 key购买 nike

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

我想测试 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 本身这一事实。通过调用方法 ArgumentCaptor.capture()verify 中间调用,Mockito 假定您实际上是在尝试验证对 capture 的调用.

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

将该注释切换为 @Captor 你应该可以开始了。

关于android - "Mockito 0 matchers expected, 3 recorded"- 为什么需要 0 个匹配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46572434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com