About the author

My photo

Java, Scala, and everything

Sunday, May 25, 2014

Validation of parameter passed to mock call in Spock

If you use Spock, sometimes you want to check mock method call. You can of course do it like this:
Problem here is that when the parameter passed to the method is wrong, you won't know what is wrong with it:
Too few invocations for:

1 * service.method({
            it.firstname == "Peter" && it.surname == "Stawicki"
        })   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * service.method(eu.vegasoft.spocktest.validateparam.Person@40f892a4)
Sometimes we want to validate also parameter passed to the mock method. We can do it like this:
Now when the passed parameter does not match, we can see exactly what was wrong with it:
Condition not satisfied:

p.firstname == "Peter"
| |         |
| Paweł     false
|           3 differences (40% similarity)
|           P(aw)e(ł)
|           P(et)e(r)