Issue
I have been struggling with ArgumentCaptor since a week, and cant find any resource or what is happening.
Simply I want to test if an user can login to my app, I do this by just calling the presenter method signInWithEmailAndPassword(email,password) , and then verifiy if the interactor callback is executed to verify it was succefully logged in, but every time I run the test I get this
java.lang.IllegalStateException: callbackCaptor.capture() must not be null
Presenter
override fun signInWithEmailAndPassword(email: String, password: String) {
view?.showProgress()
signInInteractor.signInWithEmailAndPassword(email, password, object : SignInInteractor.SignInCallBack {
override fun onSuccess() {
if (isViewAttached()) {
view?.navigateToMain()
}
}
override fun onFailure(errormsg: String) {
if (isViewAttached()) {
view?.hideProgress()
view?.showError(errormsg)
}
}
})
}
PresenterTest
@Captor
private lateinit var callbackCaptor: ArgumentCaptor<SignInInteractor.SignInCallBack>
@Mock
lateinit var view: LoginContract.View
@Mock
lateinit var interactor:SignInInteractor
@InjectMocks
lateinit var presenter: LoginPresenter
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
presenter.attachView(view)
}
@Test
fun shouldLoginIn(){
presenter.signInWithEmailAndPassword("[email protected]","asdasd")
verify(interactor,times(1)).signInWithEmailAndPassword("[email protected]","asdasd",callbackCaptor.capture())
callbackCaptor.value.onSuccess()
verify(view,times(1)).navigateToMain()
}
SignInCallback
interface SignInInteractor {
interface SignInCallBack {
fun onSuccess()
fun onFailure(errormsg:String)
}
fun signInWithEmailAndPassword(email:String,password:String,listener:SignInCallBack)
}
And my
Interactor
override fun signInWithEmailAndPassword(email:String, password:String, listener: SignInInteractor.SignInCallBack) {
FirebaseAuth.getInstance()?.signInWithEmailAndPassword(email, password)
?.addOnCompleteListener { task ->
if (task.isSuccessful) {
listener.onSuccess()
} else {
listener.onFailure(it.exception.message)
}
}
}
The problem is at my test, is not letting me check if the parameters I executed the method signInWithEmailAndPassword(...) returns onSuccess()
Error is at this line
verify(interactor,times(1)).signInWithEmailAndPassword("[email protected]","asdasd",callbackCaptor.capture())
PD: I already have asserted with no-null the ArgumentCaptor but is not working either
Solution
You should consider using mockito-kotlin for writing the tests in Kotlin.
The callbackCaptor.capture() handles the capturing internally and might just return null. That seems to be the case here as well, while you actually require a non-null type. To work around this you can use the capture(ArgumentCaptor<T>) function of mockito-kotlin, which makes sure to not return null:
verify(interactor,times(1)).signInWithEmailAndPassword(
"[email protected]",
"asdasd",
capture(callbackCaptor)
)
Answered By - tynn

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.