gpt4 book ai didi

android - 使用 mockito 和事件监听器进行单元测试 MVP

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:07:36 25 4
gpt4 key购买 nike

Android Studio 2.1.2

我想测试是否确实调用了 LoginModelImp 中的回调 onUsernameError、onPasswordError 和 onSuccess。我不确定如何测试事件监听器。但是,测试失败,因为这些函数从未被调用过。我正在用 mockito mock 他们并试图验证他们。

到目前为止,这是我的代码。

演示者界面

public interface LoginPresenterContract<LoginFragmentViewContract> {
void validateCredentials();

void attachView(LoginFragmentViewContract view);
void detachView();
}

Presenter 实现

public class LoginPresenterImp implements LoginPresenterContract<LoginFragmentViewContract>, LoginModelContract.OnLoginCompletedListener {

private LoginModelContract mLoginModelContract;
private LoginFragmentViewContract mLoginFragmentView;

public LoginPresenterImp(LoginModelContract loginModelContract) {
mLoginModelContract = loginModelContract;
}

/*
* LoginPresenterContact - implementation
*/
@Override
public void attachView(LoginFragmentViewContract view) {
mLoginFragmentView = view;
}

@Override
public void detachView() {
mLoginFragmentView = null;
}

@Override
public void validateCredentials() {
if(mLoginModelContract != null) {
mLoginModelContract.login(
mLoginFragmentView.getUsername(),
mLoginFragmentView.getPassword(),
LoginPresenterImp.this);
}
}

/*
* LoginModelContract.OnLoginCompletedListener - implementation
*/
@Override
public void onUsernameError() {
if(mLoginFragmentView != null) {
mLoginFragmentView.onLoginFailed("Incorrect username");
}
}

@Override
public void onPasswordError() {
if(mLoginFragmentView != null) {
mLoginFragmentView.onLoginFailed("Incorrect password");
}
}

@Override
public void onSuccess() {
if(mLoginFragmentView != null) {
mLoginFragmentView.onLoginSuccess();
}
}
}

模型界面

public interface LoginModelContract {
interface OnLoginCompletedListener {
void onUsernameError();
void onPasswordError();
void onSuccess();
}
void login(String username, String password, OnLoginCompletedListener onLoginCompletedListener);
}

模型实现

public class LoginModelImp implements LoginModelContract {
/* Testing Valid username and passwords */
private static String validUsername = "steve";
private static String validPassword = "1234";

@Override
public void login(final String username,
final String password,
final OnLoginCompletedListener onLoginCompletedListener) {

boolean hasSuccess = true;
if(TextUtils.isEmpty(username) || !username.equals(validUsername)) {
/* TEST onUsernameError() */
onLoginCompletedListener.onUsernameError();
hasSuccess = false;
}

if(TextUtils.isEmpty(password) || !password.equals(validPassword)) {
/* TEST onPasswordError() */
onLoginCompletedListener.onPasswordError();
hasSuccess = false;
}

if(hasSuccess) {
/* TEST onSuccess() */
onLoginCompletedListener.onSuccess();
}
}
}

使用 Mockito 进行 JUnit4 测试

public class LoginPresenterImpTest {
private LoginFragmentViewContract mMockViewContract;
private LoginModelContract mMockModelContract;
private LoginModelContract.OnLoginCompletedListener mMockOnLoginCompletedListener;
private LoginPresenterContract<LoginFragmentViewContract> mLoginPresenterContract;

@Before
public void setUp() throws Exception {
mMockViewContract = Mockito.mock(LoginFragmentViewContract.class);
mMockModelContract = Mockito.mock(LoginModelContract.class);
mMockOnLoginCompletedListener = Mockito.mock(LoginModelContract.OnLoginCompletedListener.class);
mLoginPresenterContract = new LoginPresenterImp(mMockModelContract);
mLoginPresenterContract.attachView(mMockViewContract);
}

@Test
public void shouldSuccessWithValidCredentials() {
when(mMockViewContract.getUsername()).thenReturn("steve");
when(mMockViewContract.getPassword()).thenReturn("1234");

mLoginPresenterContract.validateCredentials();

verify(mMockViewContract, times(1)).getUsername();
verify(mMockViewContract, times(1)).getPassword();

verify(mMockOnLoginCompletedListener, times(1)).onSuccess();

verify(mMockOnLoginCompletedListener, never()).onPasswordError();
verify(mMockOnLoginCompletedListener, never()).onUsernameError();
}
}

有什么方法可以测试这个实现吗?

非常感谢您的任何建议,

最佳答案

测试类 LoginPresenterImpTest 是关于 LoginPresenterImp 类的测试,它应该只使用它的实际实现和它的合作者的模拟。 LoginModelContract.OnLoginCompletedListener 类是 LoginModelImp 的协作者,因此在 LoginPresenterImp 的精心设计的纯单元测试中,就像您的一样,它是它从未被调用是完全正常的。我建议的解决方案是单独测试 LoginModelImp:

public class LoginModelImpTest {

private LoginModelContract.OnLoginCompletedListener mMockOnLoginCompletedListener;
private LoginModelImp loginModelImp;

@Before
public void setUp() throws Exception {
mMockOnLoginCompletedListener = Mockito.mock(LoginModelContract.OnLoginCompletedListener.class);
loginModelImp = new LoginModelImp();
}

@Test
public void shouldSuccessWithValidCredentials() {

loginModelImp.login("steve", "1234", mMockOnLoginCompletedListener);;

verify(mMockOnLoginCompletedListener, times(1)).onSuccess();

verify(mMockOnLoginCompletedListener, never()).onPasswordError();
verify(mMockOnLoginCompletedListener, never()).onUsernameError();
}
}

或者,您必须在 LoginPresenterImpTest 中使用 LoginModelImp 的实际实现并监视您的监听器(即演示器本身)或配置模拟以进行他们调用听众。这是一个示例,但我不会使用这个示例:

public class LoginPresenterImpTest {
private LoginFragmentViewContract mMockViewContract;
private LoginModelContract mModelContract;
private LoginModelContract.OnLoginCompletedListener mMockOnLoginCompletedListener;
private LoginPresenterContract<LoginFragmentViewContract> mLoginPresenterContract;

@Before
public void setUp() throws Exception {
mMockViewContract = Mockito.mock(LoginFragmentViewContract.class);
mModelContract = new LoginModelImp();
LoginPresenterImp spyPresenterImp = Mockito.spy(new LoginPresenterImp(mModelContract));
mLoginPresenterContract = spyPresenterImp;
mMockOnLoginCompletedListener = spyPresenterImp;
mLoginPresenterContract.attachView(mMockViewContract);
}

@Test
public void shouldSuccessWithValidCredentials() {
when(mMockViewContract.getUsername()).thenReturn("steve");
when(mMockViewContract.getPassword()).thenReturn("1234");

mLoginPresenterContract.validateCredentials();

verify(mMockViewContract, times(1)).getUsername();
verify(mMockViewContract, times(1)).getPassword();

verify(mMockOnLoginCompletedListener, times(1)).onSuccess();

verify(mMockOnLoginCompletedListener, never()).onPasswordError();
verify(mMockOnLoginCompletedListener, never()).onUsernameError();
}
}

关于android - 使用 mockito 和事件监听器进行单元测试 MVP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38300973/

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