简体   繁体   English

Grails-如何对addTo *进行单元测试

[英]Grails - How to Unit Test addTo*

Is it possible to unit test addTo* functions in Grails ? 可以在Grails中对addTo *函数进行单元测试吗?

thanks for your help. 谢谢你的帮助。

The documentation says in section 9.1 : 文档在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. 在Grails中,您需要特别注意单元测试和集成测试之间的区别,因为在单元测试中,Grails 不会注入集成测试期间和运行时出现的任何动态方法。

You either have to use mockDomain(DomainClassName) in a unit test or write an integration test: 您要么必须在单元测试中使用mockDomain(DomainClassName) ,要么编写集成测试:

Grails decorates domain object with some Dynamic methods when the DomainClassGrailsPlugin gets setup( doWithDynamicMethods ). DomainClassGrailsPlugin获得setup( doWithDynamicMethods )时,Grails用一些Dynamic方法装饰域对象。

I hit this problem in upgrading from Grails 2.1.2 to Grails 2.3.x. 从Grails 2.1.2升级到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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM