lundi 29 février 2016

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

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

Aucun commentaire:

Enregistrer un commentaire