title: Unit Testing failing with kotlin IllegalStateException
body: <p>I'm trying to verify that when I call my createUser interactor in my presenter, a user is created into Firebase. </p>
  
  <p>To do this I simply implemented this test case</p>
  
  <h2>RegisterTest.class</h2>
  
  <pre><code>    @Captor
      var callbackCaptor: ArgumentCaptor&lt;RegisterInteractor.RegisterCallBack&gt;? = 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()
          }
  </code></pre>
  
  <h2>RegisterInteractor Callback</h2>
  
  <pre><code>interface RegisterInteractor {
  
      interface RegisterCallBack {
          fun onRegistrationSucces()
          fun onRegistrationFailure(errorMsg:String)
      }
  
      fun createUserWithEmailAndPassword(fullName:String,email:String,password:String,listener:RegisterCallBack)
  }
  </code></pre>
  
  <h2>RegisterPresenter</h2>
  
  <p>Here is where I call the signUp method to test</p>
  
  <pre><code> 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()
                      }
                  }
  
              })
      }
  </code></pre>
  
  <h2>RegisterInteractorImpl</h2>
  
  <pre><code>  override fun createUserWithEmailAndPassword(fullName:String,email:String,password:String,listener: RegisterInteractor.RegisterCallBack){
          FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
              .addOnCompleteListener { task -&gt;
                  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 -&gt;
                              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!!)
                  }
              }
      }
  </code></pre>
  
  <p>And I'm getting a <strong>IllegalStateException</strong> when executing my test, this is the following message I receive</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: callbackCaptor!!.capture() must not
    be null</p>
    
    <p>at
    com.logintest.presentation.register.presenter.RegisterPresenterTest.should_SignUpUser(RegisterPresenterTest.kt:123)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)     at
    org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at
    org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at
    org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at
    org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at
    org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)    at
    org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at
    org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)  at
    org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)   at
    org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)     at
    org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)   at
    org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)  at
    org.junit.runners.ParentRunner.run(ParentRunner.java:363)     at
    org.junit.runner.JUnitCore.run(JUnitCore.java:137)    at
    com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at
    com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at
    com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at
    com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)</p>
  </blockquote>
  
  <p>And the line it refers the error is on the test</p>
  
  <pre><code>verify(interactor).createUserWithEmailAndPassword("userTest","userEmail@gmail.com","123456", callbackCaptor!!.capture())
  </code></pre>
  
  <p>Where is this error coming from ?</p>
  
  <p>Thanks</p>