如何更改每个测试的类的metaClass [英] How to change a class's metaClass per test
问题描述
我使用ExpandoMetaClass使服务始终在集成测试中返回成功,但我希望有一个测试实际上失败。
使用示例ExpandoMetaClass:
$ p $ static $ {
ExpandoMetaClass someService = new ExpandoMetaClass(Object,false)
someService.accessAnotherSystem = {return'success'}
someService.initialize()
SomeService.metaClass = someService
}
注意:目前该服务没有为控制器定义,但是因为它是一个Spring bean,它引用了名为 SomeService
的类,比如 someService.accessAnotherSystem()
工作得很好,即控制器中没有 def someService
。
因此,我不能在集成测试中执行 controller.someService.metaClass.accessAnotherSystem = {return'failure'}
。
另外请注意:这个i这是一个webflow的集成测试。
是否可以重置metaClass进行一次测试,或者在某种程度上测试我想要的结果?
如果测试是单独运行的,下面的工作正常,但是如果在它之前运行使用SomeService的另一个测试,则重写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('method_I_Want_failure')
{
result ='例外'
}
}
返回结果
}
$ b $ 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
}
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?
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
}
这篇关于如何更改每个测试的类的metaClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!