Not run methods beforeEachTest and afterEachTest kotlin spek android

231 Views Asked by At

I'm trying to write a test using Spek framework on android. But I'm very confused. In accordance with the code, the presenter must be initialized in method beforeEachTest. And the condition must be cleared in method afterEachTest. But when I run the test, it failed an error NPE in line mPresenter!!.setPass(PASSWORD, PASSWORD). But if you uncomment the two lines in front of it, then the test will succeed. What could be the problem? Thanks in advance for answers

object ResetPasswordPresenterTestKotlin : Spek({

    include(RxSchedulersOverrideSpek)

    val PASSWORD = "123456aZ";

    var mView = mock<ResetPasswordView>{}

    var mMockDataManager = mock<DataManager>{}

    var mToken = mock<SuccessDetails>{}

    var mPresenter : ResetPasswordPresenter ?= null

    beforeEachTest {
        print("beforeEachTest")
        mPresenter = ResetPasswordPresenter(mMockDataManager)
        mPresenter!!.attachView(mView)
    }

    afterEachTest {
        print("afterEachTest")
        mPresenter!!.detachView()
        reset(mView)
    }


    describe("Reset password is succesful") {
        given(mMockDataManager.setNewPassword(any())).willReturn(Observable.just(mToken))
        /*mPresenter = ResetPasswordPresenter(mMockDataManager)
        mPresenter!!.attachView(mView)*/
        mPresenter!!.setPass(PASSWORD, PASSWORD)
        it("show loading progress") {
            verify(mView).showLoadingProgress()
        }
        it("hide loading progress") {
            verify(mView).hideLoadingProgress()
        }
        it("show on success") {
            verify(mView).onSuccess()
        }
        it("no show error") {
            verify(mView, never()).showError(RuntimeException())
        }
    }


})
1

There are 1 best solutions below

0
On

Looks like you asked on github and got an answer: https://github.com/spekframework/spek/issues/265#issuecomment-331200634 . Leaving it here for others who have the same problem.