concept collaborator in category groovy

appears as: collaborators, collaborators, collaborator
Groovy in Action, Second Edition

This is an excerpt from Manning's book Groovy in Action, Second Edition.

A number of bad programming habits can make testing difficult. One is writing incoherent classes and methods that do more than they should, resulting in overly long classes and methods. Even worse is code with many dependencies to other classes that we’ll call collaborators. Unit testing your subject under test (SUT) in its purist form means that you test it in isolation without the collaborators so you’re focused on finding errors in your code.[12]

12 Other kinds of integration tests should pick up errors that come from integrating your code with the collaborators.

Finally, it’s also possible to react to the method argument that the SUT passes to the collaborator’s method. Each argument of the method call is passed into the demand closure and can thus be evaluated inside it. Suppose you expect that the stubbed method is called only with even numbers, and you’d like to assert that invariant while testing. You can achieve this with

someStub.demand.methodOne {
    number -> assert 0 == number % 2
    return 1
}

Of course, you can also combine all these kinds of demand declarations, producing an elaborate specification of call sequences on the collaborator and returned values. The more elaborate that specification is, the more likely it is that you’ll want to also assert that all demanded method calls happened. For stubs, this isn’t asserted by default, but you can enforce this check by calling