Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:49:41+00:00 2026-06-10T19:49:41+00:00

I’m writing a unit test for a Grails controller that renders a domain class

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:49:43+00:00Added an answer on June 10, 2026 at 7:49 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:
I want to show the soap response to UIWebview.. my soap response is, <p><img
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.