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:
- Create a mock of some simple Java interface such as
Runnable - Verify the interface method was called e.g.
1 * runnable.run()inside thewhen:block - You can see all the unmatched interactions
- Add all the unmatched interactions you want to verify
- 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()
}
}