我正在使用ExpandoMetaClass使服务在集成测试中始终返回成功,但我希望有一个实际失败的测试。
ExpandoMetaClass的用法示例:
static {
ExpandoMetaClass someService = new ExpandoMetaClass(Object, false)
someService.accessAnotherSystem = { return 'success' }
someService.initialize()
SomeService.metaClass = someService
}因此,我不能从集成测试中执行controller.someService.metaClass.accessAnotherSystem = { return 'failure'}。
另请注意:这是一个webflow的集成测试。
发布于 2014-12-23 23:40:54
如果测试是自己运行的,则下面的测试运行得很好,但是,如果在它之前运行了另一个使用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 = '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
复制相似问题