Q:

Grails controller testing with multiple services mock

I'm adding a test suite to an old grails project (grails 2.2.5).
I've some controllers that uses two or more services, so I need to mock them in the test body, e.g.:
void testSave() {
    def myService1 = mockFor(MyService1)
    // ... mock setup
    def myService2 = mockFor(MyService2)
    // ... mock setup
    def myService3 = mockFor(MyService3)
    // ... mock setup
    // ... test code
}

void testUpdate() {
    def myService1 = mockFor(MyService1)
    // ... mock setup
    def myService2 = mockFor(MyService2)
    // ... mock setup
    def myService3 = mockFor(MyService3)
    // ... mock setup
    // ... test code
}

// ... other methods

Since I need this service in all controller's tests, I must duplicate this code in every test method or there is a better way to do this (without code repeating)?

A:

You can make the service variables class fields/properties and then set then up in either the setup() or setupSpec() methods. Take a look at the fixture method listed here.

