项目-Junit5单元测试

项目-Junit5单元测试_第1张图片
项目-Junit5单元测试_第2张图片
项目-Junit5单元测试_第3张图片
项目-Junit5单元测试_第4张图片
public class ResetPasswordPresenterTest extends BaseTest {

    @Mock
    private ResetPasswordView mResetPasswordView;
    private AccountLogic mAccountLogic;

    private ResetPasswordPresenter mResetPasswordPresenter;
    private String fakeoriginPassword = "";
    private String fakenewPassword = "";
    private String fakeconfirmPassword = "";

    @BeforeEach
    public void setUp() {
        super.setup();
        MockitoAnnotations.initMocks(this);
        mAccountLogic = Mockito.mock(AccountLogic.class);

        mResetPasswordPresenter = new ResetPasswordPresenter(mResetPasswordView);
        mResetPasswordPresenter.mAccountLogic = mAccountLogic;
    }

    @DisplayName("onOriginPasswordChangedIsEmpty")
    @Test
    public void onOriginPasswordChangedIsEmpty() {
        mResetPasswordPresenter.onOriginPasswordChanged(fakeoriginPassword);
        Mockito.verify(mResetPasswordView).disableConfirmButton();
    }

    @DisplayName("onOriginPasswordChanged")
    @Test
    public void onOriginPasswordChanged() {
        mResetPasswordPresenter.newPassword = "newPassword";
        mResetPasswordPresenter.confirmPassword = "newPassword";
        mResetPasswordPresenter.onOriginPasswordChanged("originPassword");
        Mockito.verify(mResetPasswordView).enableConfirmButton();
    }

    @DisplayName("onNewPasswordChangedIsEmpty")
    @Test
    public void onNewPasswordChangedIsEmpty() {
        mResetPasswordPresenter.onNewPasswordChanged(fakenewPassword);
        Mockito.verify(mResetPasswordView).disableConfirmButton();
    }

    @DisplayName("onNewPasswordChanged")
    @Test
    public void onNewPasswordChanged() {
        mResetPasswordPresenter.originPassword = "originPassword";
        mResetPasswordPresenter.confirmPassword = "newPassword";
        mResetPasswordPresenter.onNewPasswordChanged("newPassword");
        Mockito.verify(mResetPasswordView).enableConfirmButton();
    }

    @DisplayName("onConfirmPasswordChangedIsEmpty")
    @Test
    public void onConfirmPasswordChangedIsEmpty() {
        mResetPasswordPresenter.onConfirmPasswordChanged(fakeconfirmPassword);
        Mockito.verify(mResetPasswordView).disableConfirmButton();
    }

    @DisplayName("onConfirmPasswordChanged")
    @Test
    public void onConfirmPasswordChanged() {
        mResetPasswordPresenter.originPassword = "originPassword";
        mResetPasswordPresenter.newPassword = "newPassword";
        mResetPasswordPresenter.onConfirmPasswordChanged("newPassword");
        Mockito.verify(mResetPasswordView).enableConfirmButton();
    }

    @DisplayName("onClickConfirm_passwordLengthIsTooShort")
    @Test
    public void onClickConfirm_passwordLengthIsTooShort() {
        mResetPasswordPresenter.newPassword = "12345";
        mResetPasswordPresenter.confirmPassword = "12345";

        Mockito.when(mAccountLogic.resetPassword("originPassword", "newPassword"))
            .thenReturn(Observable.empty());

        mResetPasswordPresenter.onClickConfirm();

        Mockito.verify(mResetPasswordView).passwordLengthIsTooShort();
    }

    @DisplayName("onClickConfirm_incorrectConfirmPassword")
    @Test
    public void onClickConfirm_incorrectConfirmPassword() {
        mResetPasswordPresenter.newPassword = "newPassword";
        mResetPasswordPresenter.confirmPassword = "confirmPassword";

        Mockito.when(mAccountLogic.resetPassword("originPassword", "newPassword"))
            .thenReturn(Observable.empty());

        mResetPasswordPresenter.onClickConfirm();

        Mockito.verify(mResetPasswordView).incorrectConfirmPassword();
    }

    @DisplayName("onClickConfirmFailed")
    @Test
    public void onClickConfirmFailed() {
        mResetPasswordPresenter.originPassword = "originPassword";
        mResetPasswordPresenter.newPassword = "newPassword";
        mResetPasswordPresenter.confirmPassword = "newPassword";

        Mockito.when(mAccountLogic.resetPassword("originPassword", "newPassword"))
            .thenReturn(Observable.error(new HttpBadRequestException()));

        mResetPasswordPresenter.onClickConfirm();

        Mockito.verify(mResetPasswordView).originPasswordIsIncorrect();
    }

    @DisplayName("onClickConfirm")
    @Test
    public void onClickConfirm() {
        mResetPasswordPresenter.originPassword = "originPassword";
        mResetPasswordPresenter.newPassword = "newPassword";
        mResetPasswordPresenter.confirmPassword = "newPassword";

        Mockito.when(mAccountLogic.resetPassword("originPassword", "newPassword"))
            .thenReturn(Observable.empty());

        mResetPasswordPresenter.onClickConfirm();

        Mockito.verify(mResetPasswordView).resetPasswordSuccess();
    }
}

官方

JUnit 5 User Guide

中文

「译」JUnit 5 系列:环境搭建
「译」JUnit 5 系列:基础入门
「译」JUnit 5 系列:架构体系
「译」JUnit 5 系列:扩展模型(Extension Model)
「译」JUnit 5 系列:条件测试

Mockito 初探

你可能感兴趣的:(项目-Junit5单元测试)