1

How to write a test (functional, unit, integration) to test a class that contains a Grails converter(s)? At the minimum I should be able to write a working integration test.

Example: The following line works fine outside of the test scope, but fails in both unit-tests and integration tests:

def json= "{}" as JSON

with GroovyCastException: Cannot cast object '{}' with class 'java.lang.String' to class 'grails.converters.JSON'. The expected outcome is that a JSON type is returned.

There seems to be the same problem with earlier versions in GRAILS-4715, however its been closed so I assume it was fixed. In Grails 3.X this issue could be circumvented like this How do I unit test a Grails service that uses a converter?. However, none of the presented workarounds seem to work in Grails 4.X. I tried manually enabling and registering the converters in the same manner it was done in Grails 3 with @TestMixin, but this was a dead end for me.

Using Grails version 4.0.3.

I also opened I ticket for this towards the Grails project https://github.com/grails/grails-core/issues/11521 hoping for a long term solution to the problem or at least some documentation on how to test this common use case of Grails converters.

john
  • 613
  • 1
  • 7
  • 25
  • The code above by default does not work in either 3.3.x or 4.0.x. I get the same exception in 3.3.9 with that. Converters usually convert domain objects to a json string, not the other way around (though I suppose you could write a custom converter to do that.) - There seems to be more going on in your app we'd need to know to help you more. – erichelgeson Apr 21 '20 at 12:24
  • @erichelgeson Seems like I oversimplified the example. I will update tomorrow. – john Apr 21 '20 at 20:49

0 Answers0