如何在模型 - 视图 - 演示者架构android中使用mockito测试演示者类

时间:2018-01-09 11:46:21

标签: android unit-testing mockito mvp presenter

我想使用mockito尝试使用mockito为演示者类创建测试,但在下面提到错误。我正在关注这些链接herehere以及我的演示者类

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.showLoading(" Loading");

请建议我如何以正确的方式实施测试用例。我在做错了。

提前致谢

1 个答案:

答案 0 :(得分:1)

在您测试的方法中,调用@Test public void signInAndShowProgress() { signInPresenter.onSignInClicked("", ""); verify(mSignInView, times(0)).showErrorMessage(Mockito.any(String.class)); verify(mSignInView).showLoading(); } 方法时没有属性..我认为您应该期望并且可能验证没有显示错误消息:

df1=df[df$type=="t1",]
df2=df[df$type=="t2",]

DF=merge(df1,df2,by="id")
DF=DF[,-c(2,5)]
colnames(DF)<-c("id", "type_t1", "val_t1","type_t2",   "val_t2")