Search code examples
groovyspock

Mockito ArgumentCaptor equivalent in spockframework


I'm beginning to write unit tests with spockframework, and I want to verify the arguments passed to some method of a class my test depends on. In Mockito we have ArgumentCaptor to do this, but I couldn't figure out a way to do that on spock.

Maybe some manipulation on the closure (I'm still trying to learn that)? Or spock have some built in functionality?

I appreciate any guidance to learn that!


Solution

  • This is how you do it in Spock:

    def "it checks the arguments passed to the helper"() {
        given:
        def cut =new Cut(helper:Mock(Helper))
    
        when:
        cut.doStuff()
    
        then:
        1 * cut.helper(expectedArg) >> returnedResult
    
        where:
        expectedArg="This is expected"
        returnedResult="xyz"
    }