开发者

How can I unit test a Grails controller when using a custom property editor?

I'd like to use the technique de开发者_C百科scribed here: Grails bind request parameters to enum to automatically bind the String representation of an enum to a domain instance. The technique works fine, but my existing controller unit tests fail because the custom editors are not loaded during unit testing. I'd hate to switch to integration tests for every controller just for this data-binding technique.

Is there a way to unit test a controller action when you have a custom property editor?


As far as I know, spring application context isnt available in unit tests, and hence your property registrars and property editors won't have been registered. so custom property editors won't work in unit tests. However, grails uses GrailsDataBinder - which is subclass of DataBinder You might be able to do some meta programming and metaClass stuff so that your custom property editor gets registered and found when bind() is called.


In Grails 2.x you can define your extra beans in your unit test, just use defineBeans as the first thing in your setup:

@TestFor(MyController)
class MyControllerTests {

  @Before
  void setup() {
    defineBeans {
      myCustomEditorRegistrar(MyCustomEditorRegistrar)
    }
  }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜