unit-testinggrailspropertyeditor

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


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?


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