Reputation: 1836
Is it possible to unit test addTo* functions in Grails ?
thanks for your help.
Upvotes: 2
Views: 1948
Reputation: 29857
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
}
Upvotes: 2
Reputation: 4459
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
).
Upvotes: 6