mockito-kotlin icon indicating copy to clipboard operation
mockito-kotlin copied to clipboard

@Captor field must be of the type ArgumentCaptor

Open szaske opened this issue 7 years ago • 2 comments
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?

szaske avatar Oct 04 '18 18:10 szaske

argumentCaptor() returns an KArgumentCaptor instance, but Mockitos @Captor expected ArgumentCaptor.

There are two ways to solve it:

  1. Use @Captor without argumentCaptor():
    @Captor
    lateinit var captor: ArgumentCaptor<DisposableObserver<List<Poi_Domain>>>
  1. Use @KCaptor of 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.

wickie73 avatar Oct 05 '18 21:10 wickie73

Why not just drop the annotation and use the argumentCaptor-function.

bohsen avatar Oct 07 '18 20:10 bohsen