如何防止服务的元类被覆盖



我试图在集成测试中模拟对外部服务的调用,该服务用于 grails webflow。该服务不在流或对话范围内,而是通过依赖项注入添加的,请参阅此处。

我已经设法找到了一种方法,通过使用 ExpandoMetaClass 替换服务的元类来覆盖服务。仅当单独运行测试时,更改才有效,如果在此测试之前运行了使用相同服务的另一个测试,则元类更改将消失。

覆盖元类的部分:

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('test_method_I_Want_failure_in')
                {
                    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
}

相关问题:如何更改每个测试的类的元类

没有办法保留我的更改以备测试,或者有没有其他方法可以覆盖服务。

如果要按测试方法覆盖测试用例中的服务,则有一种更简单的方法。看一个例子:

class SomeControllerSpec extends Specification {
    def someService
    void "test any external method for success response"() {
        given: "Mocked service method"
        someService.metaClass.accessAnotherSystem = { arg1, arg2 ->
            return "some success response"
        }
        when: "Any controller method is called which calls this service method"
        // Your action which calls that service method
        then: "Result will processed coming from mocked method"
        // Your code
    }
}

您可以对任何服务测试方法执行相同的操作。如果你想模拟你正在编写测试用例的相同服务的方法,那么在这里哟去。

class SomeServiceSpec extends Specification {
    def someService
    void "test external method call"() {
        given: "Mocked service method"
        someService.metaClass.methodA = { arg1, arg2 ->
            return "some success response"
        }
        when: "A method is called which invokes the another method"
        // Your another service method which call the same method
        someService.methodB()    // Where methodB() invokes the methodA() internally in your code
        then: "Result will processed coming from mocked method"
        // Your code
    }
}

最新更新