@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");
}
}
我对此测试有问题。
在第四次测试中应该是"密码不相同",但是当我运行测试时它仍然通过。
我知道视图有问题,因为当我将第四测试移至第一个测试时它将显示错误消息,告诉我正确的是"密码不同的"非错误"。
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");
}
}
验证(模拟(调用之间有一些干扰。
测试应仅出于一个原因而失败,因此您应该将测试分为单个测试。
之类的东西@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");
}
}