When I’m writing Spock’s specifications for legacy code I usually want to see all the interactions with mock objects happening during the…


The Easiest Way How to Display All Spock’s Mock Interactions

When I’m writing Spock’s specifications for legacy code I usually want to see all the interactions with mock objects happening during the test execution. I can use 0 * _ to verify no interactions happen but it will not print all the interactions but just the first unexpected one. For that reason, I’ve started to use a simple trick:

  1. Create a mock of some simple Java interface such as Runnable
  2. Verify the interface method was called e.g. 1 * runnable.run() inside the when: block
  3. You can see all the unmatched interactions
  4. Add all the unmatched interactions you want to verify
  5. Delete the mock and the verification

Here is a complete example:

import spock.lang.Specification

class MySpec extends Specification {

    Runnable runnable = Mock(Runnable)

    _// ... other mocks and setup_        void 'my test'() {
        when:
            _// my service call_        then:
            1 \* runnable.run()
    }

}