如何在模型-视图-演示器架构 Android 中使用 Mockito 测试演示器类



我想使用 mockito 为演示者类创建一个测试,尝试了几种方法,但在下面提到了错误。我在这里和这里以及我的演讲者课程中关注这些链接

public class SignInPresenterImpl extends BasePresenterImpl implements SignInPresenter {
    private SignInView mSignInView;
    private SignInInteractor mSignInInteractor;
    /**
     * Constructor
     *
     * @param signInView the associated SignIn view
     */
    public SignInPresenterImpl(@NonNull final SignInView signInView) {
        mSignInView = signInView;
        mSignInInteractor = new SignInInteractorImpl();
    }
    @Override
    public void onSignInClicked(final String email, final String password) {
        // checking for validation
        if (!ValidationUtil.checkEmail(email)) {
            mSignInView.showErrorMessage(R.string.error_invalid_email);
            return;
        }
        if (!ValidationUtil.checkPassword(password)) {
            mSignInView.showErrorMessage(R.string.error_invalid_password);
            return;
        }
        mSignInView.showLoading();
        mSignInInteractor.login(email, password, new BaseInteractor.ApiListener() {
            @Override
            public void onSuccess(final CommonResponse commonResponse) {
                //todo handle success
            }
            @Override
            public void onFailure(final ApiError apiError, final Throwable throwable) {
                if (isViewAttached()) {
                    mSignInView.hideLoading();
                    if (apiError != null) {
                        mSignInView.showErrorMessage(apiError.getMessage());
                    } else {
                        // resolve error through throwable
                        mSignInView.showErrorMessage(parseThrowableMessage(throwable));
                    }
                }
            }
        });
    }
}

我的演示者测试类是

public class SignInPresenterImplTest {
    @Mock
    BaseInteractor.ApiListener listener;
    @Mock
    private SignInView mSignInView;
    @Mock
    private SignInInteractor mSignInInteractor;
    private SignInPresenter signInPresenter;
    @Before
    public void setUp() throws Exception {
        // 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 refrence to the class test.
        signInPresenter = new SignInPresenterImpl(mSignInView);
    }
    @Test
    public void signInAndShowProgress() {
        signInPresenter.onSignInClicked("", "");
        mSignInView.showErrorMessage("");
        verify(mSignInView).showLoading("Loading");
    }
}

mSignInView 显示以下错误

需要但未调用:mSignInView.showLoad("Load"(;

请建议我如何以正确的方式实现测试用例,我在其中做错了什么。

提前致谢

在测试的方法中,调用showLoading方法时没有属性。 我认为您应该预料到这一点,并可能验证没有显示任何错误消息:

@Test
public void signInAndShowProgress() {
    signInPresenter.onSignInClicked("", "");
    verify(mSignInView, times(0)).showErrorMessage(Mockito.any(String.class));
    verify(mSignInView).showLoading();
}

相关内容

  • 没有找到相关文章

最新更新