简体   繁体   English

Grails单元测试和bootstrap

[英]Grails unit testing and bootstrap

I wrote an unit test for a controller. 我为控制器写了一个单元测试。

I have a Bootstrap file which alter the metaclass of domain classes by adding a method asPublicMap() . 我有一个Bootstrap文件,它通过添加一个方法asPublicMap()来改变域类的元类。 I use this method in the controller to return domain classes as json but only some selected public fields. 我在控制器中使用此方法将域类作为json返回,但只返回一些选定的公共字段。

My unit test failed because of MissingMethodException for asPublicMap() . 由于asPublicMap()MissingMethodException ,我的单元测试失败了。 As I understood, bootstrap classes are not loaded for unit tests, only for integration tests. 据我所知,bootstrap类不会加载单元测试,仅用于集成测试。 That's why I got this error. 这就是我得到这个错误的原因。

My question is : Is there another place to put metaclass manipulation in order to take them into account during unit tests ? 我的问题是:在单元测试期间是否还有其他地方可以使用元类操作来考虑它们?

Simply resolved by moving the unit test into an integration test. 通过将单元测试移动到集成测试中来解决问题。 Bootstrap classes are loaded for integration test, so now it works well. 加载Bootstrap类进行集成测试,所以现在它运行良好。

My concern on that migration was about the @Mock annotation, but surprisingly it works also in integration tests. 我对迁移的关注是关于@Mock注释的,但令人惊讶的是它也适用于集成测试。 The database isn't used for mocked domain classes. 该数据库不用于模拟的域类。

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

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