Quantcast

Testing "render x as XML" with mock objects?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Testing "render x as XML" with mock objects?

Jean-Henri Duteau
I have a controller where I'm simply rendering the retrieved objects as XML:
    def show() {
if (params.id) {
def conf = Conformance.findById(params.id)
if (conf == null) {
render "No Conformance"
} else {
render conf as XML
}
} else {
def all = Conformance.list()
render all as XML
}
}

When I run the unit test on this, I get a conversion error.  After debugging, it appears that it's because the renderer doesn't like the MockObject proxy that is being passed to it:

org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class org.springframework.beans.GenericTypeAwarePropertyDescriptor
at grails.converters.XML.render(XML.java:115)
at grails.converters.XML.render(XML.java:252)
at org.codehaus.groovy.grails.plugins.converters.api.ConvertersControllersApi.render(ConvertersControllersApi.groovy:40)
at com.healthfire.ConformanceController.show(ConformanceController.groovy:17)
at com.healthfire.ConformanceControllerSpec.convert all conformances to XML(ConformanceControllerSpec.groovy:24)

Caused by: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class org.springframework.beans.GenericTypeAwarePropertyDescriptor
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:65)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:33)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.ArrayMarshaller.marshalObject(ArrayMarshaller.java:41)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.ArrayMarshaller.marshalObject(ArrayMarshaller.java:29)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:47)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:33)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:47)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:33)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GroovyBeanMarshaller.marshalObject(GroovyBeanMarshaller.java:48)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GroovyBeanMarshaller.marshalObject(GroovyBeanMarshaller.java:34)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.CollectionMarshaller.marshalObject(CollectionMarshaller.java:43)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.CollectionMarshaller.marshalObject(CollectionMarshaller.java:31)
at grails.converters.XML.convertAnother(XML.java:169)
at grails.converters.XML.render(XML.java:111)
... 4 more

Caused by: java.lang.IllegalAccessException: Class org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller can not access a member of class org.springframework.beans.GenericTypeAwarePropertyDescriptor with modifiers "public"
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:45)
... 22 more

Here is my test in case I'm doing something quite silly (note that it's a Spock test):
@TestFor(ConformanceController)
@Mock(Conformance)
class ConformanceControllerSpec extends grails.plugin.spock.ControllerSpec {

    def "convert all conformances to XML"() {
setup:
mockDomain(Conformance, [
new Conformance(date: new Date(), fhirVersion: "0.5", narrative: "test conformance 1"), 
new Conformance(date: new Date(), fhirVersion: "0.5", narrative: "test conformance 2"), 
new Conformance(date: new Date(), fhirVersion: "0.5", narrative: "test conformance 3")
])
when:
controller.show()
then:
response.contentType == "text/xml"
    }



Thanks,

Jean Duteau
Director
Duteau Design Inc
Bus: 780-328-6395
Cell: 780-937-8991

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Testing "render x as XML" with mock objects?

Roberto Guerra
I similar issue with JSON. It is one of the most frustrating aspects of Grails. I'm about to finish a blog post about it. In short, this is what I did.
I created a JsonService that uses groovy's native JsonBuilder. This converts my objects to JSON. I call this service from my controller and pass it to the render method:

render(contentType : "application/json"{
    jsonService.render(myDomainInstance)
}

Then my tests pass. 

I noticed that the issue only happens when testing. If you do run-app, then grails works fine when I do render domainInstance as JSON. 

On Thu, Jul 26, 2012 at 2:55 AM, Jean-Henri Duteau <[hidden email]> wrote:
I have a controller where I'm simply rendering the retrieved objects as XML:
    def show() {
if (params.id) {
def conf = Conformance.findById(params.id)
if (conf == null) {
render "No Conformance"
} else {
render conf as XML
}
} else {
def all = Conformance.list()
render all as XML
}
}

When I run the unit test on this, I get a conversion error.  After debugging, it appears that it's because the renderer doesn't like the MockObject proxy that is being passed to it:

org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class org.springframework.beans.GenericTypeAwarePropertyDescriptor
at grails.converters.XML.render(XML.java:115)
at grails.converters.XML.render(XML.java:252)
at org.codehaus.groovy.grails.plugins.converters.api.ConvertersControllersApi.render(ConvertersControllersApi.groovy:40)
at com.healthfire.ConformanceController.show(ConformanceController.groovy:17)
at com.healthfire.ConformanceControllerSpec.convert all conformances to XML(ConformanceControllerSpec.groovy:24)

Caused by: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class org.springframework.beans.GenericTypeAwarePropertyDescriptor
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:65)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:33)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.ArrayMarshaller.marshalObject(ArrayMarshaller.java:41)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.ArrayMarshaller.marshalObject(ArrayMarshaller.java:29)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:47)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:33)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:47)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:33)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GroovyBeanMarshaller.marshalObject(GroovyBeanMarshaller.java:48)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GroovyBeanMarshaller.marshalObject(GroovyBeanMarshaller.java:34)
at grails.converters.XML.convertAnother(XML.java:169)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.CollectionMarshaller.marshalObject(CollectionMarshaller.java:43)
at org.codehaus.groovy.grails.web.converters.marshaller.xml.CollectionMarshaller.marshalObject(CollectionMarshaller.java:31)
at grails.converters.XML.convertAnother(XML.java:169)
at grails.converters.XML.render(XML.java:111)
... 4 more

Caused by: java.lang.IllegalAccessException: Class org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller can not access a member of class org.springframework.beans.GenericTypeAwarePropertyDescriptor with modifiers "public"
at org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller.marshalObject(GenericJavaBeanMarshaller.java:45)
... 22 more

Here is my test in case I'm doing something quite silly (note that it's a Spock test):
@TestFor(ConformanceController)
@Mock(Conformance)
class ConformanceControllerSpec extends grails.plugin.spock.ControllerSpec {

    def "convert all conformances to XML"() {
setup:
mockDomain(Conformance, [
new Conformance(date: new Date(), fhirVersion: "0.5", narrative: "test conformance 1"), 
new Conformance(date: new Date(), fhirVersion: "0.5", narrative: "test conformance 2"), 
new Conformance(date: new Date(), fhirVersion: "0.5", narrative: "test conformance 3")
])
when:
controller.show()
then:
response.contentType == "text/xml"
    }



Thanks,

Jean Duteau
Director
Duteau Design Inc
Bus: <a href="tel:780-328-6395" value="+17803286395" target="_blank">780-328-6395
Cell: <a href="tel:780-937-8991" value="+17809378991" target="_blank">780-937-8991




--
The Journey Is The Reward.
Loading...