gpt4 book ai didi

android - Mockito 和回调返回 "Argument(s) are different!"

转载 作者:太空狗 更新时间:2023-10-29 16:32:29 25 4
gpt4 key购买 nike

我正在尝试在 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

谢谢你的帮助=)

编辑

我尝试测试的方法(LoginPresenter):

@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 方法比较参数。除非你定义了一个灵活的 equals 实现你的用户对象,这很可能会失败。

考虑使用isA(User.class),它将简单地验证对象instanceof User,或any()anyObject() 完全跳过匹配第一个参数。

关于android - Mockito 和回调返回 "Argument(s) are different!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35441489/

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