mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.11k stars 202 forks source link

@Captor field must be of the type ArgumentCaptor #297

Open szaske opened 6 years ago

szaske commented 6 years ago

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?

wickie73 commented 6 years ago

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>>>
  2. 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.

bohsen commented 6 years ago

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