Quantcast

2.4.1 - change in metaclass override behavior with regard to private methods

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

2.4.1 - change in metaclass override behavior with regard to private methods

Jay Ashworth
Hi, 

I'm trying to upgrade from Groovy 2.3.7 to the shiniest version, 2.4.1, but I'm running into a problem where my unit tests are no longer functioning.  What's happening, I believe is that the metaClass overrides that I'm defining in the unit test aren't actually being executed instead of the 'production' logic.  I believe it's specific to the visibility of the methods I'm overriding, as they fail when 'private' but succeed when visibility is shifted to 'public'. 

When running unit tests I have a setup like: 

class MyLogic { 
  void myMethod() { 
     .... 
       def dataObj = doInternalDBCall() 
     ... 
   } 

   private DBObject doInternalDBCall() { 
     ....  // real DB interaction 
   } 


and a unit test 

class MyLogicTest { 
  @Test void testMyMethod() { 
     MyLogic.metaClass.doInternalDBCall = {  new DBObject() }  // populated with test data, instead of actually calling to a database 

      ....  // asserts, etc 
   } 


This fails to override the production logic when executing the unit test, as I had been expecting it.  Is this a desired/known change?  I don't want to make the internal logic public so (for me, at least) it was a nice feature that Groovy was able to override the implementation details even for private methods.  

There are other approaches I can take to work around this, but I'd like to know if this is something that might be considered a bug (and thereby 'fixed' in the future), or if it's intended behavior. 


Thank you for your time! 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 2.4.1 - change in metaclass override behavior with regard to private methods

Jay Ashworth
Ugh - I think this is a duplicate.  I had asked via the nabble interface, but wasn't sure it had been published through to the mailing list, so sent it via email.  (I now think it had been correctly pushed to the mailing list...)

Sorry for the repeat question!

Although, I still don't have an answer, so if you happen to respond, then thank you again for your time. :)
Loading...