Search code examples
grailsspock

When to use mock and when to use stub with spock?


I want to understand more this sentence found in Grails documentation:

If the test is concerned with proving that the test subject interacts with a collaborator in a particular way, use a mock. If the fact that a collaborator behaves in a certain way exposes a particular behavior in the test subject the outcome of that behavior is what you are testing, use a stub


Solution

  • If you'd like to test how many times some method will be called use Mock:

    2 * yourService.someMethod(*_) // test that someMethod will be called 2 times
    

    If no - use Stub