Hi David,
Maybe I'm not fully awake yet, but I offered a suggestion on the related SO question.

-Keegan

On Fri, Aug 28, 2015 at 3:20 AM, David Smith <mail@smith9.com> wrote:

I am trying to get multiple mocks working in groovy. The only way I have managed to get this working is to create my own kind of mock - adding a meta method.

I have tried using nested use statements and also tried one use and one proxy with verify, neither of which worked. Both of these returned a failure - "junit.framework.AssertionFailedError: No more calls to 'pop' expected at this point. End of demands."

import groovy.mock.interceptor.MockFor
import org.junit.Test

class MockTest {
    // results in No more calls to 'pop' expected at this point. End of demands.
    @Test
    public void testMock() {
        MockFor pupilMock = new MockFor(Pupil)
        MockFor bubbleMock = new MockFor(SomeService)
        GroovyObject bubbleProxy = bubbleMock.proxyInstance()
        pupilMock.demand.blowBubble { String colour ->
            return bubbleProxy
        }
        bubbleMock.demand.pop {}
        pupilMock.use {
            bubbleMock.use {
                Teacher teacher = new Teacher()
                teacher.lesson("red")
            }
        }
    }
    // results in No more calls to 'pop' expected at this point. End of demands.
    @Test
    public void testProxy() {
        MockFor pupilMock = new MockFor(Pupil)
        MockFor bubbleMock = new MockFor(SomeService)
        GroovyObject bubbleProxy = bubbleMock.proxyInstance()
        pupilMock.demand.blowBubble { String colour ->
            return bubbleProxy
        }
        bubbleMock.demand.pop {}
        pupilMock.use {
                Teacher teacher = new Teacher()
                teacher.lesson("red")
        }
        bubbleMock.verify(bubbleProxy)
    }
    // only using a single mock so works
    @Test
    public void testMetaclass() {
        MockFor pupilMock = new MockFor(Pupil)
        SomeService.metaClass.pop = { println "pop was called" }
        SomeService metaBubble = new SomeService("red")
        pupilMock.demand.blowBubble { String colour ->
            return metaBubble
        }
        pupilMock.use {
                Teacher teacher = new Teacher()
                teacher.lesson("red")
        }
    }
}

class Teacher {
    public void lesson(String colour) {
        Pupil pupil = new Pupil()
        SomeService bubble = pupil.blowBubble(colour)
        bubble.pop()
    }
}

class Pupil {
    SomeService blowBubble(String colour) {
        SomeService child = new SomeService(colour)
        return child
    }
}

class SomeService {
    String colour

    SomeService(String colour) {
        this.colour = colour
    }
    void pop() {
        println "popped ${colour}"
    }
}