groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Smith <m...@smith9.com>
Subject Issue trying to use multiple MockFor
Date Fri, 28 Aug 2015 07:20:21 GMT
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.MockForimport 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}"
    }}

Mime
View raw message