Bad groovy MockFor and grails UnitTestMixin interaction

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Bad groovy MockFor and grails UnitTestMixin interaction

Eric Sword
I am upgrading the unit tests of an existing app.  In a few places we use the groovy MockFor class rather than the grails mockFor method.  We do this mainly because we need the MockFor.use(closure) which is not available through the mockFor method.  The problem I am seeing is that the metaclass and/or demands structure put in place by the MockFor class do not appear to be cleaned up after the test is complete.  Any subsequent tests that use the class which was the target of MockFor get demand violations when attempting to call methods on the target class.  This not only happens with tests in the same test class, but also test methods in other unit tests and even in integration tests if they are all run together.

Has anyone else seen this, or does anyone have an idea for how to work around it?

tx,

e
Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

alxndrsn
Hi Eric,

I've not really worked with Grails 2, but calling `registerMetaClass
MyClassForMocking` at the very start of your test might be worth a
try.

Alex

On 22 March 2012 17:18, Eric Sword <[hidden email]> wrote:

> I am upgrading the unit tests of an existing app.  In a few places we use
> the groovy MockFor class rather than the grails mockFor method.  We do this
> mainly because we need the MockFor.use(closure) which is not available
> through the mockFor method.  The problem I am seeing is that the metaclass
> and/or demands structure put in place by the MockFor class do not appear to
> be cleaned up after the test is complete.  Any subsequent tests that use the
> class which was the target of MockFor get demand violations when attempting
> to call methods on the target class.  This not only happens with tests in
> the same test class, but also test methods in other unit tests and even in
> integration tests if they are all run together.
>
> Has anyone else seen this, or does anyone have an idea for how to work
> around it?
>
> tx,
>
> e

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

bendoerr
In reply to this post by Eric Sword
Eric,

What version of Grails are you having issues with? I've experience similar problems with Grails 2.0.0 where the GrailsMock was causing metaClasses to not get cleaned up. And I've still seen instances of metaClass not getting cleaned up using GrailsMock in Grails 2.0.1.  In one of the early RC's of 2.0 there was a similar issue but it was only happening on Windows.

I have not seen issues with groovy's MockFor since it typically uses proxies rather than metaClasses. I'm still convinced there are issues with the new MetaClassregistryCleaner OR groovy's MetaClassRegistryChangeEventListener, but I haven't had time to investigate.

Again, I'm not sure why groovy's MockFor is failing for you since my understanding was that it uses proxies. But I would also advise against using registerMetaClass in Grails 2.0 based on my experience since it has a possibility of causing the MetaClassregistryCleaner to registrar your changed metaClass as the original (at least it did in the early RC's) and the  MetaClassRegistryChangeEventListener should pick up all changes to metaClasses.

As an aside, I feel your pain, when you run into these issues it's hair pulling. Our philosophy for unit testing has us writing unit tests often without using the "Grails Way" of unit testing, meaning we are more concerned with the messages being sent to our collaborators and the result of our method under test given certain responses from those collaborators. The "Grails Way" has you setting up conditions that meet certain requirements to meet code coverage. Since 2.0, it's become useful in that we can move away from integration tests for testing the correctness of queries but the "Grails Way" is really useless to us when it comes to our beliefs in unit testing. IE your unit test should have no idea what findAllBy... is going to do, but the "Grails Way" has you setting up conditions to return what you need from that. Anyway sorry for the side rant.

If you have time to setup a test/sample app that exposes this behavior I think everyone in the community who has these issues would really appreciate it. I think I could convince my team that we should investigate it, if it were isolated.

-Ben Doerr
Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

bksaville
In reply to this post by Eric Sword
Make sure that you are using either registerMetaClass (if you are extending GrailsUnitTest class still), or use the TestFor/Mock annotations on the test class (no registerMetaClass is available in this case).  If you're testing a class that isn't a grails artefact, meaning you can't use TestFor, then use this format:

import grails.test.mixin.*
import grails.test.mixin.support.*

@TestMixin(GrailsUnitTestMixin)
class ...


Hopefully that helps,
Brian

On Thu, Mar 22, 2012 at 8:18 AM, Eric Sword <[hidden email]> wrote:
I am upgrading the unit tests of an existing app.  In a few places we use the groovy MockFor class rather than the grails mockFor method.  We do this mainly because we need the MockFor.use(closure) which is not available through the mockFor method.  The problem I am seeing is that the metaclass and/or demands structure put in place by the MockFor class do not appear to be cleaned up after the test is complete.  Any subsequent tests that use the class which was the target of MockFor get demand violations when attempting to call methods on the target class.  This not only happens with tests in the same test class, but also test methods in other unit tests and even in integration tests if they are all run together.

Has anyone else seen this, or does anyone have an idea for how to work around it?

tx,

e

Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

Eric Sword
This is with Grails 2.0.1 on 32-bit Fedora 14 with sun/oracle java 1.6.21 underneath.  I have moved all of the tests over to use the TestFor/Mock annotations, though I first noticed the problem when there was a mix of new-style tests (with annotations) and old style tests (extending GrailsUnitTestCase).  I'll see if I can pull out a sample app that shows the issue.  Truly bizarre.  I have also seen very strange behavior if I have a base test class with a couple of properties and initialization-methods (annotated with @Before) that other test classes extend.  I'll post another thread about that one.
Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

Roshan Dawrani
In reply to this post by bendoerr
On Thu, Mar 22, 2012 at 8:26 PM, bendoerr <[hidden email]> wrote:
Eric,

What version of Grails are you having issues with? I've experience similar
problems with Grails 2.0.0 where the GrailsMock was causing metaClasses to
not get cleaned up. And I've still seen instances of metaClass not getting
cleaned up using GrailsMock in Grails 2.0.1.

+1. I am also facing this meta-classes not being cleaned-up in unit tests with the use of mockFor in Grails 2.0.1!

Cheers.

Roshan
Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

bksaville
+1 as well, although I've only noticed it with a mix of the old style
extending GrailsUnitTest and spock tests using the TestFor/Mock mixins.

-Brian

From: Roshan Dawrani [mailto:[hidden email]]
Sent: Monday, March 26, 2012 7:28 AM
To: [hidden email]
Subject: Re: [grails-user] Re: Bad groovy MockFor and grails UnitTestMixin
interaction

On Thu, Mar 22, 2012 at 8:26 PM, bendoerr <[hidden email]> wrote:
Eric,

What version of Grails are you having issues with? I've experience similar
problems with Grails 2.0.0 where the GrailsMock was causing metaClasses to
not get cleaned up. And I've still seen instances of metaClass not getting
cleaned up using GrailsMock in Grails 2.0.1.

+1. I am also facing this meta-classes not being cleaned-up in unit tests
with the use of mockFor in Grails 2.0.1!

Cheers.

Roshan
http://roshandawrani.wordpress.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

Graeme Rocher
Administrator
In reply to this post by Roshan Dawrani
On Mon, Mar 26, 2012 at 3:27 PM, Roshan Dawrani <[hidden email]> wrote:

> On Thu, Mar 22, 2012 at 8:26 PM, bendoerr <[hidden email]> wrote:
>>
>> Eric,
>>
>> What version of Grails are you having issues with? I've experience similar
>> problems with Grails 2.0.0 where the GrailsMock was causing metaClasses to
>> not get cleaned up. And I've still seen instances of metaClass not getting
>> cleaned up using GrailsMock in Grails 2.0.1.
>
>
> +1. I am also facing this meta-classes not being cleaned-up in unit tests
> with the use of mockFor in Grails 2.0.1!

Attach an example to a JIRA issue please

Cheers

>
> Cheers.
>
> Roshan
> http://roshandawrani.wordpress.com/



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

itomatik
+1 facing same issue.
anyone figured out a clean way to resolve this?
Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

w4tson
I've recently started using the GRAILS stack (2.1.0) at work.

Everything was going so well until I hit this exact problem.

I'm using the groovy "new MockFor(...)" syntax in my unit tests to mock our some collaborators to my Grails service.

These mocks are then bleeding through into my integration tests as mentioned in this thread.

Does anyone know if this bug is being addressed or a decent workaround?

Reply | Threaded
Open this post in threaded view
|

Re: Bad groovy MockFor and grails UnitTestMixin interaction

danielsweb
This post was updated on .
I'm facing the same issue. I've written more about it here:

http://grails.1312388.n4.nabble.com/Grails-2-Testing-Mock-once-always-mocked-td4644441.html#a4644487

I'm researching the problem.

Edit: This is happening with Grails 2.2.2.