mockito-kotlin
mockito-kotlin copied to clipboard
@Captor field must be of the type ArgumentCaptor
trafficstars
I'm trying to write a unit test using your library version 2.0.0-RC2 and have the following code:
@Captor
val captor = argumentCaptor<DisposableObserver<List<Poi_Domain>>>()
and it gives me a error: "org.mockito.exceptions.base.MockitoException: @Captor field must be of the type ArgumentCaptor. Field: 'captor' has wrong type"
what am I doing wrong?
argumentCaptor() returns an KArgumentCaptor instance, but Mockitos @Captor expected ArgumentCaptor.
There are two ways to solve it:
- Use
@CaptorwithoutargumentCaptor():
@Captor
lateinit var captor: ArgumentCaptor<DisposableObserver<List<Poi_Domain>>>
- Use
@KCaptorof mockito4kotlin.annotation:
import org.mockito4kotlin.annotation.KCaptor
import org.mockito4kotlin.annotation.MockAnnotations
@KCaptor
lateinit var captor: KArgumentCaptor<DisposableObserver<List<Poi_Domain>>>
fun setUp() {
MockAnnotations.initMocks(this)
}
or if you like with argumentCaptor():
import org.mockito4kotlin.annotation.KCaptor
import org.mockito4kotlin.annotation.MockAnnotations
@KCaptor
var captor = argumentCaptor<DisposableObserver<List<Poi_Domain>>>()
fun setUp() {
MockAnnotations.initMocks(this)
}
Read more about @KCapture vs. @Captor Annotation
Hope I could help.
Why not just drop the annotation and use the argumentCaptor-function.