groovy - Saving argument method with MOP -
i doing integration tests spock 3rd party apps. struggling problem not sure wether approaching issue or not.
in 1 of tests connecting 3rd party service information in array. each of these items passed method process them individually.
def get3rdpartyitems = { [item1, item2, item3] } def processitem = { item -> //do item } get3rdpartyitems.each { processitem(it) }
then have test connects real 3rd party service using method get3rdpartyitems() in testing processitem called many times items has returned method get3rdpartyitems().
what trying save 1 of items @shared variable write test know item processed don't want mock content retrieved 3rd party service want real data.
basically, doing:
@shared def globalitem myclass.metaclass.processitem = { -> if (!globalitem) globalitem = //and need call original method processitem }
any clue how achieve this? overheading open change solution.
not sure if want, it's hard see existing structure code , code isn't runnable as-is, given class:
class myclass { def get3rdpartyitems = { ['item1', 'item2', 'item3'] } def processitem( item ) { println item //do item } def run() { get3rdpartyitems().each { processitem( ) } } }
you can this:
def globalitem def oldprocessitem = myclass.metaclass.getmetamethod("processitem", object) myclass.metaclass.processitem = { item -> if (!globalitem) { println "setting global item $item" globalitem = item } oldprocessitem.invoke( delegate, item ) } def mc = new myclass() new myclass().run()
Comments
Post a Comment