55

Consider a function that takes an interface implementation as an argument like this:

interface Callback {
    fun done()
}

class SomeClass {        

    fun doSomeThing(callback: Callback) {

        // do something

        callback.done()

    }    
}

When I want to test the caller of this function, I can do something like

val captor = ArgumentCaptor.forClass(Callback::class)
Mockito.verify(someClass).doSomeThing(captor.capture())

To test what the other class does when the callback is invoked, I can then do

captor.value.done()

Question: How can I do the same if I replace the callback interface with a high order function like

fun doSomeThing(done: () -> Unit) {

    // do something

    done.invoke()

}

Can this be done with ArgumentCaptor and what class do I have to use in ArgumentCaptor.forClass(???)

4 Answers 4

90

I recommend nhaarman/mockito-kotlin: Using Mockito with Kotlin

It solves this through an inline function with a reified type parameter:

inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)

Source: mockito-kotlin/ArgumentCaptor.kt at a6f860461233ba92c7730dd42b0faf9ba2ce9281 · nhaarman/mockito-kotlin

e.g.:

val captor = argumentCaptor<() -> Unit>()
verify(someClass).doSomeThing(captor.capture())

or

val captor: () -> Unit = argumentCaptor()
verify(someClass).doSomeThing(captor.capture())
9
  • I already have this library included in my project, could you provide an example how to use the ArgumentCaptor to invoke a function that was passed to a mock?
    – fweigl
    Aug 2, 2016 at 20:19
  • 39
    I am getting captor.capture() must not be null :( Jan 30, 2017 at 21:07
  • 1
    @JesusAlmaral You can create a new question on this site with appropriate labels, code samples, etc. to get help showing what you've tried and what error messages you are getting, etc.
    – mfulton26
    Jan 30, 2017 at 21:09
  • 7
    The wiki from mockito-kotlin has some interesting syntax for using captors: github.com/nhaarman/mockito-kotlin/wiki/…
    – Hay
    Mar 2, 2018 at 22:43
  • 1
    I have the same code but strangely the compilation error says that the method doesn't match any of the existing implementation. I'm trying to capture the request from restTemplate as bellow: val captor = argumentCaptor<() -> HttpEntity<String>>() and Mockito.verify(restTemplate).exchange("testUrl", HttpMethod.POST, captor.capture(), String::class.java) Is there anything I did wrong?
    – xbmono
    Jul 6, 2022 at 6:51
5

I tried what @mfulton26 suggested, but was getting an error message saying captor.capture() must not be null. and this was what worked for me.

Declared a member variable captor with @Captor annotation,

@Captor private lateinit var captor: ArgumentCaptor<Callback>

and in my @Test,

verify(someClass).doSomething(capture(captor))
2
  • 14
    This returns java.lang.IllegalStateException: callbackCaptor.capture() must not be null Apr 30, 2019 at 21:55
  • 1
    To deal with must not be null issue object MockitoHelper { fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture() } for complete ref check this link derekwilson.net/blog/2018/10/19/mockito-kotlin-2 Mar 3, 2021 at 14:52
4

I had this problem just now and solved it with an inline argumentCaptor from mockito-kotlin:

argumentCaptor<String>().apply {
  verify(myClass, times(2)).setItems(capture())

  assertEquals(2, allValues.size)
  assertEquals("test", firstValue)
}

firstValue is a reference to the first captured object.

Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-and-verifying#argument-captors

2

Based on mfulton26's answer, i create an example below.

to show how to invoke the captured function or lambda expression.

you need the mockito-kotlin


Assume we have a Class A, it has a suspend function with two higher order function as parameters.

how can we mock the onSuccess scenario and onError scenario

class A {
    suspend fun methodB(onSuccess: (ModelA) -> Unit, onError: (ErrorA) -> Unit)
}

Here is the dummy example

// in the unit test class

private val mockClassA = // use annotation or mock()

// decalre the higer oder function capture variables.
private val onSuccessCapture = argumentCaptor<(ModelA) -> Unit>()
private val onErrorCapture = argumentCaptor<(ErrorA) -> Unit>()


@Test
fun testMethodB = testDispatcher.runBlockingTest {

   doAnswer {
       // on success scenario 
       val modelA = // get ModelA
       onSuccessCapture.firstValue.invoke(modelA) // this line will let the onSuccess parameter been called

       // on error scenario
       // val errorA = // get ErrorA
       //onErrorCapture.firstValue.invoke(errorA)

   }.`when`(mockClassA).methodB(onSuccessCapture.capture(), onErrorCapture.capture())
   
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.