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.