How to change a class's metaClass per test

2019-08-30 14:41发布

问题:

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
}