timbonicus
timbonicus

Reputation: 908

JSON rendering fails for domain objects in ControllerUnitTestCase

I'm writing a unit test for a Grails controller that renders a domain class to a JSON response:

class MyController {
    def find = {
        def domainInst = MyDomainClass.get(params.id)
        render ([data: domainInst] as JSON)
    }
}

The unit test extends ControllerUnitTestCase and provides a mock for the domain object:

class MyControllerTests extends ControllerUnitTestCase {
    @Before
    void setUp() {
        super.setUp()
        mockDomain(MyDomainClass, [new MyDomainClass(id: 7)])
    }

    @Test
    void testFind() {
        def inst = MyDomainClass.get(7)
        controller.params.id = inst.id
        controller.find()
        assert(controller.response.json.data.id == inst.id)
    }

This all seems to be working nicely except for the JSON rendering, which spits out a nasty stack trace:

| Failure:  testFind(MyControllerTests)
|  org.apache.commons.lang.UnhandledException: 
        org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class MyDomainClass
        Caused by: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class MyDomainClass
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.convertAnother(JSON.java:162)
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.render(JSON.java:134)
... 5 more
        Caused by: java.lang.reflect.InvocationTargetException
... 9 more
        Caused by: groovy.lang.MissingMethodException: No signature of method: MyDomainClass.isAttached() is applicable for argument types: () values: []
        Possible solutions: isAttached(), attach()
... 9 more

Changing the return to a Map instead of a domain class works:

render ([data: [id: domainInst.id]] as JSON)

What's causing the JSON marshaller to die on the domain class? It works in a normal environment, but not in the mock test environment. Is there a way to make this test work?

Upvotes: 3

Views: 3451

Answers (1)

Daniel Woods
Daniel Woods

Reputation: 1029

Looks like you might need to do some fine tuning to make the converters realize that you're trying to render a domain class as a JSON object. It works when you manually put your id into a map because it is rendering the response from a Map object instead of a Grails domain class, which needs to go through a special ObjectMarshaller.

Something like this:

// Domain Class
class Foo {
    String foo
}

// Controller class
class MyController {
    def find = {
        def domainInst = Foo.get(params.id)
        render domainInst as JSON
    }
}

// Controller Test Class
class MyControllerTests extends ControllerUnitTestCase {

    static application

    @Before
    void setUp() {
        super.setUp()

        // Register some common classes so that they can be converted to XML, JSON, etc.
        def convertersInit = new ConvertersConfigurationInitializer()
        convertersInit.initialize(application)
        [ List, Set, Map, Errors ].each { addConverters(it) }
        def xmlErrorMarshaller = new ValidationErrorsMarshaller()
        XML.registerObjectMarshaller(xmlErrorMarshaller)
        def jsonErrorMarshaller = new ValidationErrorsMarshaller()
        JSON.registerObjectMarshaller(jsonErrorMarshaller)

        ApplicationHolder.application.addArtefact("Domain", Foo)
        mockDomain(Foo, [new Foo(foo: "foo")] )
    }

    @Test
    void testJSON() {
        def inst = Foo.list()[0]
        controller.params.id = inst.id
        def model = controller.find()
        assert controller.response.json.foo == "foo"
    }

    @Override
    protected def bindMockWebRequest(GrailsMockHttpServletRequest mockRequest, GrailsMockHttpServletResponse mockResponse) {
        MockApplicationContext ctx = new MockApplicationContext()
        application = new DefaultGrailsApplication([testClass] as Class[], getClass().classLoader)
        application.initialise()
        ctx.registerMockBean("grailsApplication", application)
        ctx.registerMockBean(testClass.name, testClass.newInstance())
        def lookup = new TagLibraryLookup(applicationContext: ctx, grailsApplication: application)
        lookup.afterPropertiesSet()
        ctx.registerMockBean("gspTagLibraryLookup", lookup)
        ctx.registerMockBean(GroovyPagesUriService.BEAN_ID, new DefaultGroovyPagesUriService())
        mockRequest.servletContext.setAttribute(ApplicationAttributes.APPLICATION_CONTEXT, ctx)
        mockRequest.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx)

        webRequest = new GrailsWebRequest(mockRequest, mockResponse, mockRequest.servletContext)

        mockRequest.setAttribute(GrailsApplicationAttributes.WEB_REQUEST, webRequest)
        RequestContextHolder.setRequestAttributes(webRequest)
    }
}

Hope this helps!

Upvotes: 1

Related Questions