单元测试因Kotlin IllegalStateException失败

时间:2019-04-29 17:38:51

标签: android unit-testing kotlin

我试图验证在演示者中调用createUser交互器时,是否已将用户创建到Firebase中。

为此,我只是实现了这个测试用例

RegisterTest.class

    @Captor
    var callbackCaptor: ArgumentCaptor<RegisterInteractor.RegisterCallBack>? = null

    @Mock
    lateinit var view: RegisterContract.View

    @Mock
    lateinit var interactor: RegisterInteractor
    lateinit var presenter: RegisterPresenter


    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        presenter = RegisterPresenter(interactor)
        presenter.attachView(view)
    }

    @Test
        fun should_SignUpUser(){
            //give
            presenter.signUp("testUser","testEmail@gmail.com","123456")
            //when
            verify(interactor).createUserWithEmailAndPassword("testUser","testEmail@gmail.com","123456", callbackCaptor!!.capture())
            callbackCaptor?.value?.onRegistrationSucces()
            //then
            verify(view).navigateToMain()
        }

RegisterInteractor回调

interface RegisterInteractor {

    interface RegisterCallBack {
        fun onRegistrationSucces()
        fun onRegistrationFailure(errorMsg:String)
    }

    fun createUserWithEmailAndPassword(fullName:String,email:String,password:String,listener:RegisterCallBack)
}

RegisterPresenter

在这里我调用signUp方法进行测试

 override fun signUp(fullName:String, email: String, password: String) {
        view?.showProgress()
        registerInteractor?.createUserWithEmailAndPassword(fullName,email, password, object : RegisterInteractor.RegisterCallBack {

                override fun onRegistrationSucces() {
                    if(isViewAttached()){
                        view?.navigateToLogin()
                        view?.hideProgress()
                    }
                }

                override fun onRegistrationFailure(errorMsg:String) {
                    if(isViewAttached()){
                        view?.showError(errorMsg)
                        view?.hideProgress()
                    }
                }

            })
    }

RegisterInteractorImpl

  override fun createUserWithEmailAndPassword(fullName:String,email:String,password:String,listener: RegisterInteractor.RegisterCallBack){
        FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    // Sign in success, set full name and navigate to main.
                    val user = FirebaseAuth.getInstance().currentUser
                    val profileUpdates = UserProfileChangeRequest.Builder()
                        .setDisplayName(fullName)
                        .build()

                    // Checks if the user is already in the database, if not, it will write it and go to the main activity
                    user?.updateProfile(profileUpdates)
                        ?.addOnCompleteListener { updateTask ->
                            if (updateTask.isSuccessful) {
                                userRepository.createUser(fullName,email,object : UserRepository.UserRepositoryCallback{

                                    override fun onRemoteSuccess() {
                                        listener.onRegistrationSucces()
                                    }

                                    override fun onRemoteFailure(errormsg:String) {
                                        // Since the task is the one who decides if an user is created or not by checking if the email is not duplicated, this callback has no effect on saving a call to Firestore
                                    }
                                })

                            }
                        }

                } else {
                    listener.onRegistrationFailure(task.exception?.message!!)
                }
            }
    }

执行测试时,我收到 IllegalStateException ,这是我收到的以下消息

  

java.lang.IllegalStateException:callbackCaptor !!。capture()不得   为空

     在

  com.logintest.presentation.register.presenter.RegisterPresenterTest.should_SignUpUser(RegisterPresenterTest.kt:123)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)处   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)     在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)     在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:290)在   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)在   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)在   org.junit.runners.ParentRunner.run(ParentRunner.java:363)在   org.junit.runner.JUnitCore.run(JUnitCore.java:137)在   com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)     在   com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)     在   com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)     在   com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

错误所在的行在测试中

verify(interactor).createUserWithEmailAndPassword("userTest","userEmail@gmail.com","123456", callbackCaptor!!.capture())

此错误来自何处?

谢谢

0 个答案:

没有答案