Question

I'd like to use the technique described 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?

Was it helpful?

Solution

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)
    }
  }

}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top