2012-02-23 104 views

回答

2

你可以metaClass方法让它返回任何你想要的。

@Test 
void pluginCode() { 
    def myService = new MyService() 
    def wasCalled = false 
    myService.metaClass.sendJMSMessage = {String message -> 
     //I like to have an assert in here to test what's being passed in so I can ensure wiring is correct 
     wasCalled = true 
     null //this is what the method will now return 
    } 

    def results = myService.myServiceMethodThatCallsPlugin() 

    assert wasCalled 
} 

我喜欢当我从metaClassed方法返回null因为我特别不喜欢声称的响应null,因为它并没有真正确保你的有线了wasCalled标志正确。如果你正在返回某种独特的东西,尽管你可以在没有wasCalled标志的情况下完成。

在上面的示例中,我使用了1个字符串参数,但您可以用metaClass取出任何数量/类型的参数以匹配实际发生的情况。

+0

嗨Jarred,不幸的是该方法不是在服务。这是我的插件“暴露”在我的服务上的东西。不完全确定它是如何工作的,因为我从来没有尝试过编写插件,但我可以简单地称这种方法为“无处不在”。 – Krystian 2012-02-23 16:30:18

+0

然后,他们可能将其分类到所有服务(可能的控制器和域对象)。那么你可以直接在你的服务上metaClass它。我会更新我的答案。 – 2012-02-23 16:54:51

相关问题