Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
metaclass: how to replace and restore a method?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Aled Sage  
View profile  
 More options Dec 2 2011, 1:29 pm
From: Aled Sage <aled.s...@cloudsoftcorp.com>
Date: Fri, 2 Dec 2011 18:29:16 +0000
Local: Fri, Dec 2 2011 1:29 pm
Subject: [groovy-user] metaclass: how to replace and restore a method?
Hi,

I'm having trouble with replacing and then restoring a method using
metaClass, to override behaviour for a test. Any help much appreciated.

In one of my tests, I inject:
         Foo.metaClass.myBusinessLogic = { /* record stuff here */ }

That works well for intercepting the (expensive) call and recording it.
But everything in the test suite then subsequently fails because the
method is now a no-op! I've been trying to restore the original method
in the tearDown.

---
My attempts have involved code like that below. But for each of my
attempts, it keeps writing out "replacement".

class ReplacingAndRestoringMetaMethod {
     public static void main(String[] args) {
         def origMethod = Foo.metaClass.myBusinessLogic
         //def origMethod2 =
Foo.metaClass.getMetaMethod("myBusinessLogic", new Object[0])

         Foo.metaClass.myBusinessLogic = { println "replacement" }
         Foo.metaClass.myBusinessLogic = origMethod

         new Foo().myBusinessLogic()
     }

}

public class Foo {
     public void myBusinessLogic() { println "orig" }

}

Thanks, Aled

p.s. I'd prefer to use
http://docs.codehaus.org/display/GROOVY/Groovy+Mocks, but the code
hasn't been written to support such injection...

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

    http://xircles.codehaus.org/manage_email


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paulo Gabriel Poiati  
View profile  
 More options Dec 2 2011, 2:03 pm
From: Paulo Gabriel Poiati <paulogpoi...@gmail.com>
Date: Fri, 2 Dec 2011 17:03:54 -0200
Local: Fri, Dec 2 2011 2:03 pm
Subject: Re: [groovy-user] metaclass: how to replace and restore a method?

One way to do this is using categories:

*class Foo {*
*
*
*    def bar() {*
*        'Original'*
*    }*
*    *
*}*
*
*
*class FooTestCategory {*
*    *
*    static String bar(Foo foo) {*
*        'Mocked'*
*    }*
*    *
*}*
*
*
*def foo = new Foo()*
*
*
*assert "Original" == foo.bar()*
*
*
*use (FooTestCategory) {*
*    assert "Mocked"== foo.bar()*
*}*
*
*
*assert "Original" == foo.bar()*

[]'s
Paulo Poiati

blog.paulopoiati.com

On Fri, Dec 2, 2011 at 4:29 PM, Aled Sage <aled.s...@cloudsoftcorp.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aled Sage  
View profile  
 More options Dec 2 2011, 5:31 pm
From: Aled Sage <aled.s...@cloudsoftcorp.com>
Date: Fri, 2 Dec 2011 22:31:56 +0000
Local: Fri, Dec 2 2011 5:31 pm
Subject: Re: [groovy-user] metaclass: how to replace and restore a method?

Thanks Paulo, that works great.

It was made a bit more complicated by my app-under-test being
multi-threaded, so I had to inject a thread factory that wraps each
Runnable.run in a `use (FooTestCategory)`. Luckily I could inject that
thread factory using another test category :-)

Thanks again for the fast response,

Aled

On 02/12/2011 19:03, Paulo Gabriel Poiati wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dinko Srkoc  
View profile  
 More options Dec 2 2011, 6:32 pm
From: Dinko Srkoc <dinko.sr...@gmail.com>
Date: Sat, 3 Dec 2011 00:32:12 +0100
Local: Fri, Dec 2 2011 6:32 pm
Subject: Re: [groovy-user] metaclass: how to replace and restore a method?
On 2 December 2011 19:29, Aled Sage <aled.s...@cloudsoftcorp.com> wrote:

> Hi,

> I'm having trouble with replacing and then restoring a method using
> metaClass, to override behaviour for a test. Any help much appreciated.

> In one of my tests, I inject:
>        Foo.metaClass.myBusinessLogic = { /* record stuff here */ }

> That works well for intercepting the (expensive) call and recording it. But
> everything in the test suite then subsequently fails because the method is
> now a no-op! I've been trying to restore the original method in the
> tearDown.

In order to restore the original method you might try putting this in
the tearDown:

  GroovySystem.metaClassRegistry.removeMetaClass Foo

This may have the same effect:

  Foo.metaClass = null

Cheers,
Dinko

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

    http://xircles.codehaus.org/manage_email


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "metaclass: how to replace and restore a static method?" by Coderextreme
Coderextreme  
View profile  
 More options Feb 18 2012, 3:38 am
From: Coderextreme <JohnWCarl...@lbl.gov>
Date: Sat, 18 Feb 2012 00:38:37 -0800 (PST)
Local: Sat, Feb 18 2012 3:38 am
Subject: [groovy-user] Re: metaclass: how to replace and restore a static method?
It looks like you have to have an instance before using categories. What do
you do if you don't have an instance, for example, I am replacing the method
static method bar in class Foo:

Foo.metaClass.'static'.bar = { "Mocked" }

What's the best way restore bar to it's original state?  Or is there a
better solution (I hope).

--
View this message in context: http://groovy.329449.n5.nabble.com/metaclass-how-to-replace-and-resto...
Sent from the groovy - user mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Coderextreme  
View profile  
 More options Feb 18 2012, 4:18 am
From: Coderextreme <JohnWCarl...@lbl.gov>
Date: Sat, 18 Feb 2012 01:18:51 -0800 (PST)
Local: Sat, Feb 18 2012 4:18 am
Subject: [groovy-user] Re: metaclass: how to replace and restore a static method?
This seems to work.  I am not sure I like it though.  Any better solutions?

class Foo {

    static def bar() {
        'Original'
    }

}

assert "Original" == Foo.bar()

Foo.metaClass.'static'.bar = {"Mocked" }

assert "Mocked" == Foo.bar()

Foo.metaClass = null

assert "Original" == Foo.bar()

Foo.metaClass.'static'.bar = {"Mocked" }

assert "Mocked" == Foo.bar()

Foo.metaClass = null

assert "Original" == Foo.bar()

--
View this message in context: http://groovy.329449.n5.nabble.com/metaclass-how-to-replace-and-resto...
Sent from the groovy - user mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Edward Sumerfield  
View profile  
 More options Feb 18 2012, 9:59 am
From: Edward Sumerfield <esume...@bitbashers.org>
Date: Sat, 18 Feb 2012 09:59:09 -0500
Local: Sat, Feb 18 2012 9:59 am
Subject: Re: [groovy-user] Re: metaclass: how to replace and restore a static method?

Yes, that has worked for me too but if you have two methods, how would you
revert just one to it's original version.

Ed
On Feb 18, 2012 4:20 AM, "Coderextreme" <JohnWCarl...@lbl.gov> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Coderextreme  
View profile  
 More options Feb 23 2012, 3:32 pm
From: Coderextreme <JohnWCarl...@lbl.gov>
Date: Thu, 23 Feb 2012 12:32:59 -0800 (PST)
Local: Thurs, Feb 23 2012 3:32 pm
Subject: [groovy-user] Re: metaclass: how to replace and restore a static method?
http://stackoverflow.com/questions/8349077/how-do-i-reset-a-mocked-st...

--
View this message in context: http://groovy.329449.n5.nabble.com/metaclass-how-to-replace-and-resto...
Sent from the groovy - user mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »