groovy per instance metaClass method override doesnt work as expected in spock test

一笑奈何 提交于 2019-12-14 01:56:44

问题


having a problem

I have a class with method called execute(). In some spock unit test i dummy out the execute method and give it a mock closure like this

def setup () {
    rule = new DynamicRule ()
}

def "test default execution " (){
    given : "basic AORule "
    def mockres
    rule.metaClass.execute = {-> mockres = "did nothing"}     //mock the action
    def res = rule.execute()


    expect : "execute should do nothing "
    mockres == "did nothing"

}

if i run this test it fails. in the idea editor it shows the mock closure as underlined. but the rule.execute() on the next line isnt - so it can see the method

if i change this test for this

    rule.metaClass.execute2 = {-> mockres = "did nothing"}     //mock the action
    def res = rule.execute2()

then the test passes.

outside of spock i just ran a simple groovy script and did the method overload and that works correctly as id expect and the method is mocked out with the closure

class A {
    def execute () {
        println "thing"
    }
}

def c = new A()
def res
c.execute()

c.metaClass.execute  = {-> res =2 ; println "modified thing "; }

c.execute ()

println "res = "+  res

why doesn't the same happen in the spock test ?

query - how do unit stub test a closure correctly for spock ?

this modified version of the test works successfully

def "test default execution " (){
    given : "basic AORule "

    def mockres

    def stub = new StubFor(AORule)
    stub.demand.execute { mockres = "did nothing" }
//        rule.metaClass.execute = {-> mockres = "did nothing"}     //mock the action
//        def res = rule.execute()

    expect : "execute should do nothing "
    stub.use {
        rule.execute()
        mockres == "did nothing"

    }
}

why didn't the simple per metaclass frig work in spock ? Something i'm not understanding here


回答1:


This is an open issue on Groovy >= 2.4.3, here: GROOVY-7368.

There is a bug in the overriding of private methods with the metaclass.



来源:https://stackoverflow.com/questions/35707386/groovy-per-instance-metaclass-method-override-doesnt-work-as-expected-in-spock-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!