[prev in list] [next in list] [prev in thread] [next in thread] 

List:       groovy-user
Subject:    [groovy-user] Issue with ExpandoMetaClass
From:       bborchardt <bborchardt () gmail ! com>
Date:       2011-08-29 17:39:51
Message-ID: 1314639591411-4747059.post () n5 ! nabble ! com
[Download RAW message or body]

Hi,

We've run into an issue attempting to stub out method calls using
ExpandoMetaClass when writing tests.  Under certain conditions the method
we're trying to replace in the metaClass is not taking effect.  I've pasted
some Groovy code below to demonstrate the problem.  Are we using
ExpandoMetaClass incorrectly or is this a defect with the way it works?

-------------------------------------------------------------
class Inner {
    String run() { 'run the real Inner' }
}
 
class Outer {
    Inner inner
    
    String runOuter1() { inner.run() }
    String runOuter2() { inner.run() }
}
 
// The following line makes the inner meta class an expando meta class.
// Without it the meta class overrides won't take effect on the existing
instance of inner.
Inner.metaClass.addNewMethod = {}

Outer outer = new Outer(inner: new Inner())

// This line causes the fake Inner to not take effect when calling
runOuter1.
// If we comment it out then everything works fine.
assert 'run the real Inner' == outer.runOuter1()
Inner.metaClass.run = { -> 'run the fake Inner' }

// this one works
assert 'run the fake Inner' == outer.runOuter2()
// this one doesn't ... why?
assert 'run the fake Inner' == outer.runOuter1()
-------------------------------------------------------------

Thanks,
Brett

--
View this message in context: \
http://groovy.329449.n5.nabble.com/Issue-with-ExpandoMetaClass-tp4747059p4747059.html \
Sent from the groovy - user mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic