3
我有一个服务,它使用jms-grails插件提供的sendJMSMessage方法。 我想写一些单元测试,但我不知道如何“嘲笑”这种方法,所以它什么也没做。 任何提示?我该如何单元测试使用插件中的方法的grails服务?
我有一个服务,它使用jms-grails插件提供的sendJMSMessage方法。 我想写一些单元测试,但我不知道如何“嘲笑”这种方法,所以它什么也没做。 任何提示?我该如何单元测试使用插件中的方法的grails服务?
你可以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
取出任何数量/类型的参数以匹配实际发生的情况。
嗨Jarred,不幸的是该方法不是在服务。这是我的插件“暴露”在我的服务上的东西。不完全确定它是如何工作的,因为我从来没有尝试过编写插件,但我可以简单地称这种方法为“无处不在”。 – Krystian 2012-02-23 16:30:18
然后,他们可能将其分类到所有服务(可能的控制器和域对象)。那么你可以直接在你的服务上metaClass它。我会更新我的答案。 – 2012-02-23 16:54:51