How to change a class's metaClass per test

余生长醉 提交于 2019-12-13 03:05:13

问题


I'm using the ExpandoMetaClass to make a service always return success in an integration test but I would like to have one test that actually fails.

Example use of ExpandoMetaClass:

static {
        ExpandoMetaClass someService = new ExpandoMetaClass(Object, false)
        someService.accessAnotherSystem = { return 'success' }
        someService.initialize()
        SomeService.metaClass = someService
    }

Note: currently the service isn't defined for the controller but since it's a spring bean referencing the class named SomeService like someService.accessAnotherSystem() works just fine i.e. there is no def someService in the controller.

Therefore I can't do controller.someService.metaClass.accessAnotherSystem = { return 'failure'} from the integration test.

Also note: this is an integration test for a webflow.

Is it possible to reset the metaClass for one test, or in someway test what I want?


回答1:


The below works fine if the test is ran by itself, however the ExpandoMetaClass is overridden if another test that uses SomeService is ran before it. I'll be opening another question for that problem.

static {
    ExpandoMetaClass someService = new ExpandoMetaClass(Object, false)
    someService.invokeMethod = { String name, args ->
        def result = 'success'
        if(name.equals('accessAnotherSystem')
        {
            StackTraceUtils.sanitize(new Throwable()).stackTrace.each
            {
                if(it.methodName.equals('method_I_Want_failure')
                {
                    result = 'exception'
                }
            }
            return result
        }

        def validMethod = SomeService.metaClass.getMetaMethod(name, args)
        if (validMethod != null)
        {
            validMethod.invoke(delegate, args)
        }
        else
        {
            SomeService.metaClass.invokeMissingMethod(delegate, name, args)
        }
    }
    someService.initialize()
    SomeService.metaClass = someService
}


来源:https://stackoverflow.com/questions/27533910/how-to-change-a-classs-metaclass-per-test

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