Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 97A51185CE for ; Fri, 28 Aug 2015 12:59:35 +0000 (UTC) Received: (qmail 20519 invoked by uid 500); 28 Aug 2015 12:59:30 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 20487 invoked by uid 500); 28 Aug 2015 12:59:30 -0000 Mailing-List: contact users-help@groovy.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.incubator.apache.org Delivered-To: mailing list users@groovy.incubator.apache.org Received: (qmail 20477 invoked by uid 99); 28 Aug 2015 12:59:30 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 28 Aug 2015 12:59:30 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id F19FDC06B6 for ; Fri, 28 Aug 2015 12:59:29 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 2.9 X-Spam-Level: ** X-Spam-Status: No, score=2.9 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HTML_MESSAGE=3, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=disabled Authentication-Results: spamd4-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id oRciGvGE_KLf for ; Fri, 28 Aug 2015 12:59:19 +0000 (UTC) Received: from mail-qg0-f51.google.com (mail-qg0-f51.google.com [209.85.192.51]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with ESMTPS id 688F221597 for ; Fri, 28 Aug 2015 12:59:18 +0000 (UTC) Received: by qgeb6 with SMTP id b6so28430953qge.3 for ; Fri, 28 Aug 2015 05:59:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=H5eZzl+QQ5HmG/FAES2DVhfowa/qoslmOIq1+ZNxhxc=; b=YjsG8z6A2wCOjthAXW3ldzwkhi3qxa8hXZqZMEI5htGaA68Okj5CcoMAUf5Lw+X6tq JLE3+wZ2WeSM8fXaT0SxyPFljjA9LeyyOt0TZsgvQPqFMgf3U5d2W7O2XeF4bNN5Ut7c AuvneLb6G8QN1LJnCtBTAqUOFfHfaZ8wADIQB+dhl/u2B0G1jKf7e+TmjDMlZVSpdBnz izaEIdglN76oOptKxqmOUxf2MLlF7sjzWdCN8csJL1z/mn8JJD+Khs1vMRPOoOYeN8eu 8LNCrNX6qQC11gr/2EG8rvVdydB9nLzyvj9ohinYRrZc4pFa1DbDGYdehJUnrnKC7M7y zMsQ== X-Received: by 10.140.216.194 with SMTP id m185mr16107334qhb.98.1440766757285; Fri, 28 Aug 2015 05:59:17 -0700 (PDT) MIME-Version: 1.0 Received: by 10.140.92.213 with HTTP; Fri, 28 Aug 2015 05:58:57 -0700 (PDT) In-Reply-To: References: From: Keegan Witt Date: Fri, 28 Aug 2015 08:58:57 -0400 Message-ID: Subject: Re: Issue trying to use multiple MockFor To: users@groovy.incubator.apache.org Content-Type: multipart/alternative; boundary=001a1135d0e8c5b75a051e5ea6ae --001a1135d0e8c5b75a051e5ea6ae Content-Type: text/plain; charset=UTF-8 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 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.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}" > }} > > --001a1135d0e8c5b75a051e5ea6ae Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi David,
Maybe I'm not fully awake yet, but I off= ered a suggestion on the related SO question.

-Keegan

On Fri, Aug 28, = 2015 at 3:20 AM, David Smith <mail@smith9.com> wrote:
<=
span style=3D"margin:0px;padding:0px;border:0px;color:rgb(0,0,139)">

I am trying to get multiple moc= ks 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. Bot= h of these returned a failure - "junit.framework.AssertionFailedError:= No more calls to 'pop' expected at this point. End of demands.&quo= t;

import groovy.mock.intercepto=
r=
.MockFor
import org.junit.Test

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


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


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


class SomeService {
    String colour

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

--001a1135d0e8c5b75a051e5ea6ae--