@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");
}
}
我的测试有问题。
第4次测试应该是“密码不一样”,但是当我运行测试时它仍然会通过。
我知道视图存在问题,因为当我将第4个测试移动到第一个测试时 它将显示错误消息,告诉我正确的是“密码不相同”而不是“错误”。
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");
}
}
答案 0 :(得分:0)
验证(模拟)调用之间存在一些干扰。
测试只会因一个原因而失败,因此您应该将测试分成单独的测试。
类似
@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");
}
}