我正在尝试在Android上使用mockito。我想通过一些回调使用它。这是我的测试:
public class LoginPresenterTest {
private User mUser = new User();
@Mock
private UsersRepository mUsersRepository;
@Mock
private LoginContract.View mLoginView;
/**
* {@link ArgumentCaptor} is a powerful Mockito API to capture argument values and use them to
* perform further actions or assertions on them.
*/
@Captor
private ArgumentCaptor<LoginUserCallback> mLoadLoginUserCallbackCaptor;
private LoginPresenter mLoginPresenter;
@Before
public void setupNotesPresenter() {
// Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
// inject the mocks in the test the initMocks method needs to be called.
MockitoAnnotations.initMocks(this);
// Get a reference to the class under test
mLoginPresenter = new LoginPresenter(mUsersRepository, mLoginView);
// fixtures
mUser.setFirstName("Von");
mUser.setLastName("Miller");
mUser.setUsername("von.miller@broncos.us");
mUser.setPassword("Broncos50superBowlWinners");
}
@Test
public void onLoginFail_ShowFail() {
// When try to login
mLoginPresenter.login("von.miller@broncos.us", "notGoodPassword");
// Callback is captured and invoked with stubbed user
verify(mUsersRepository).login(eq(new User()), mLoadLoginUserCallbackCaptor.capture());
mLoadLoginUserCallbackCaptor.getValue().onLoginComplete(eq(mUser));
// The login progress is show
verify(mLoginView).showLoginFailed(anyString());
}
但是我得到了这个错误:
Argument(s) are different! Wanted:
mUsersRepository.login(
ch.example.project.Model.User@a45f686,
<Capturing argument>
);
-> at example.ch.project.Login.LoginPresenterTest.onLoginFail_ShowFail(LoginPresenterTest.java:94)
Actual invocation has different arguments:
mUsersRepository.login(
ch.example.project.Model.User@773bdcae,
ch.example.project.Login.LoginPresenter$1@1844b009
);
也许问题是第二个实际参数是ch.example.project.Login.LoginPresenter$1@1844b009?
我跟着:https://codelabs.developers.google.com/codelabs/android-testing/#5
谢谢你的帮助=)
编辑
我尝试测试的方法(登录演示器):
@Override
public void login(String email, String password) {
mLoginView.showLoginInProgress();
User user = new User();
user.setUsername(email);
user.setPassword(password);
mUsersRepository.login(user, new UsersRepository.LoginUserCallback() {
@Override
public void onLoginComplete(User loggedUser) {
mLoginView.showLoginComplete();
}
@Override
public void onErrorAtAttempt(String message) {
mLoginView.showLoginFailed(message);
}
});
}
eq(new User())
当使用eq
(或根本不使用匹配器)时,Mockito使用传入实例的equals
方法来比较参数。除非为 User 对象定义了灵活的equals
实现,否则此操作很可能会失败。
考虑使用 isA(User.class)
,这将简单地验证对象是否instanceof User
,或者any()
或anyObject()
完全跳过匹配的第一个参数。
我在 rxjava 2 和 dagger 2 中使用 mvp 模式,并且被困在使用 Mockito 对演示者进行单元测试时。给我"参数不同!错误:
@Mock
ImageService imageService;
@Mock
MetadataResponse metadataResponse;
private String imageId = "123456789";
@Test
public void getImageMetadata() {
when(imageService.getImageMetadata(imageId)).thenReturn(Observable.just(Response.success(metadataResponse)));
presenter.getImageMetaData(imageId);
verify(view).showImageData(new ImageData()));
}
这将引发如下错误消息:
论点不同!需要:实际调用不同 参数:com.example.model.ImageData@5q3v861
感谢@Jeff鲍曼的回答,在我更改此行后它起作用
verify(view).showImageData(new ImageData()));
跟
verify(view).showImageData(isA(ImageData.class));