开发者

Grails - How to Unit Test addTo*

Is it possible to unit test addTo* functions in Grails ?

开发者_如何学编程

thanks for your help.


The documentation says in section 9.1:

In Grails you need to be particularity aware of the difference between unit and integration tests because in unit tests Grails does not inject any of the dynamic methods present during integration tests and at runtime.

You either have to use mockDomain(DomainClassName) in a unit test or write an integration test:

Grails decorates domain object with some Dynamic methods when the DomainClassGrailsPlugin gets setup(doWithDynamicMethods).


I hit this problem in upgrading from Grails 2.1.2 to Grails 2.3.x. Where as before you only needed to mock the domain class you are adding to, now you need to also mock the domain class being added. Simple with Annotations.

@TestFor(YourService)
@Mock([MyClass, MyOtherClass])
class YourServiceTests {
  .... //now myClass.addToMyOtherClasses(myOtherClassInstance) should work fine in your test or in the code being tested
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜