简体   繁体   中英

Android unit test with Mockito

@RunWith(MockitoJUnitRunner.class)

public class RegisterMemberPresenterTest {
    @Mock
    private  RegisterModel mRegisterModel;
    @Mock
    private  VerifyModel mVerifyModel;
    @Mock
    private  RegisterMemberContract.View mView;
    @Mock
    private  RegisterMemberPresenter mRegisterMemberPresenter;
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }
    @Test
    public void check_validPassword(){
        mRegisterMemberPresenter = new RegisterMemberPresenter(mRegisterModel,mVerifyModel,mView);
         mMemberPresenter.validPassword2("","123");
      verify(mView).setPasswordValidation(false, "error");

      mMemberPresenter.validPassword2("123","");
      verify(mView).setPassword2Validation(false, "error");

      mMemberPresenter.validPassword2("123","123");
      verify(mView).setPassword2Validation(true, null);

      mMemberPresenter.validPassword2("123","456");
      verify(mView).setPassword2Validation(false, "error");
    }
}

I have a problem with this test.

It should be "password is not same" on the 4th test, but it still passes when I run the test.

I know there is a problem with view, because when I move the 4th test to the first It will show the error message telling me the correct is "password is not same" not "error".

public void validPassword2(String pwd, String pwd2) {
    if (pwd.isEmpty()) {
        mView.setPasswordValidation(false, "error");
        return;
    }
    if (pwd2.isEmpty()) {
        mView.setPassword2Validation(false, "error");
        return;
    }
    if (pwd.equals(pwd2)) {
        mView.setPassword2Validation(true, null);
    } else {
        mView.setPassword2Validation(false, "password is not same");
    }
}

there is some interference going on between the verify(mock) calls.

Tests should fail for one reason only and for that reason you should split your tests into individual tests.

something like

@RunWith(MockitoJUnitRunner.class)

public class RegisterMemberPresenterTest {
    @Mock
    private  RegisterModel mRegisterModel;
    @Mock
    private  VerifyModel mVerifyModel;
    @Mock
    private  RegisterMemberContract.View mView;
    @Mock
    private  RegisterMemberPresenter mRegisterMemberPresenter;
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mRegisterMemberPresenter = new RegisterMemberPresenter(mRegisterModel,mVerifyModel,mView);
    }

    @Test
    public void check_validPassword_blankFirstPassword(){
         mMemberPresenter.validPassword2("","123");
         verify(mView).setPasswordValidation(false, "error");
    }

    @Test
    public void check_validPassword_blankSecondPassword(){
         mMemberPresenter.validPassword2("123","");
         verify(mView).setPassword2Validation(false, "error");
    }

    @Test
    public void check_validPassword_validMatchingPasswords(){
         mMemberPresenter.validPassword2("123","123");
         verify(mView).setPassword2Validation(true, null);
    }

    @Test
    public void check_validPassword_nonMatchingPasswords(){
         mMemberPresenter.validPassword2("123","456");
         verify(mView).setPassword2Validation(false, "error");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM