grails-marshallers icon indicating copy to clipboard operation
grails-marshallers copied to clipboard

Add Documentation on how to use it in a unit test for a controller with usage of deep objects

Open langerc opened this issue 10 years ago • 2 comments

I use Objects that include others with the deep option. included the configuration in my Domain Objects marshalling = { .. } configuration.

moreover i added the following to my controller setup() (from the unit test case in the project)

    grailsApplication.registerArtefactHandler(new JsonMarshallerArtefactHandler())
    grailsApplication.registerArtefactHandler(new XmlMarshallerArtefactHandler())
    defineBeans {
        convertersConfigurationInitializer(ConvertersConfigurationInitializer)
        extendedConvertersConfigurationInitializer(ExtendedConvertersConfigurationInitializer)
    }

and initialize the test before i call the action in the controller private def initialize(){ grailsApplication.mainContext.convertersConfigurationInitializer.initialize(grailsApplication) grailsApplication.mainContext.extendedConvertersConfigurationInitializer.initialize() }

After adding this the objects get marshalled as json in a correct way. I use the serializer feature to serialize enums to their names instead of the enum object to have a nice json - when i try to verify the enum in a unit test it is working in the root object but not in the objects included with deep, on those objects the unconverted enum value is returned. When i use the same object in a real controller - the serializer works.

dont know if its a bug or if i left something out in the config - cant find any clues on how to unit test a controller the proper way.

langerc avatar Sep 18 '14 15:09 langerc

On 18.09.2014 17:53, langerc wrote:

I use Objects that include others with the deep option. included the configuration in my Domain Objects marshalling = { .. } configuration.

moreover i added the following to my controller setup() (from the unit test case in the project)

| grailsApplication.registerArtefactHandler(new JsonMarshallerArtefactHandler()) grailsApplication.registerArtefactHandler(new XmlMarshallerArtefactHandler()) defineBeans { convertersConfigurationInitializer(ConvertersConfigurationInitializer) extendedConvertersConfigurationInitializer(ExtendedConvertersConfigurationInitializer) } |

and initialize the test before i call the action in the controller private def initialize(){ grailsApplication.mainContext.convertersConfigurationInitializer.initialize(grailsApplication) grailsApplication.mainContext.extendedConvertersConfigurationInitializer.initialize() }

After adding this the objects get marshalled as json in a correct way. I use the serializer feature to serialize enums to their names instead of the enum object to have a nice json - when i try to verify the enum in a unit test it is working in the root object but not in the objects included with deep, on those objects the unconverted enum value is returned. When i use the same object in a real controller - the serializer works.

dont know if its a bug or if i left something out in the config - cant find any clues on how to unit test a controller the proper way.

What do you get as an output? Did you maybe forgot to claim all required domain classes as mocked collaborators?

— Reply to this email directly or view it on GitHub https://github.com/pedjak/grails-marshallers/issues/23.

dhalupa avatar Sep 18 '14 16:09 dhalupa

On 18.09.2014 17:53, langerc wrote:

I use Objects that include others with the deep option. included the configuration in my Domain Objects marshalling = { .. } configuration.

moreover i added the following to my controller setup() (from the unit test case in the project)

| grailsApplication.registerArtefactHandler(new JsonMarshallerArtefactHandler()) grailsApplication.registerArtefactHandler(new XmlMarshallerArtefactHandler()) defineBeans { convertersConfigurationInitializer(ConvertersConfigurationInitializer) extendedConvertersConfigurationInitializer(ExtendedConvertersConfigurationInitializer) } |

and initialize the test before i call the action in the controller private def initialize(){ grailsApplication.mainContext.convertersConfigurationInitializer.initialize(grailsApplication) grailsApplication.mainContext.extendedConvertersConfigurationInitializer.initialize() }

After adding this the objects get marshalled as json in a correct way. I use the serializer feature to serialize enums to their names instead of the enum object to have a nice json - when i try to verify the enum in a unit test it is working in the root object but not in the objects included with deep, on those objects the unconverted enum value is returned. When i use the same object in a real controller - the serializer works.

dont know if its a bug or if i left something out in the config - cant find any clues on how to unit test a controller the proper way.

— Reply to this email directly or view it on GitHub https://github.com/pedjak/grails-marshallers/issues/23.

One other thing.. notice there is an invocation of initialize() private method from each GenericDomainClassJSONMarshallerUnitSpec which actually iterates through domain classes and builds marshalling configuration. I admit, it could be done better

dhalupa avatar Sep 18 '14 16:09 dhalupa