Error using as JSON converter in a service in a unit test

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Error using as JSON converter in a service in a unit test

bjfish
Hello, 

I am getting the following error when trying to serialize a list to JSON using as JSON in a class:

| Failure:  testSomething(jsoningroovy.JustatestServiceTests)
|  org.apache.commons.lang.UnhandledException: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Unconvertable Object of class: java.util.ArrayList
at jsoningroovy.JustatestService.serviceMethod(JustatestService.groovy:13)
at jsoningroovy.JustatestServiceTests.testSomething(JustatestServiceTests.groovy:20)
Caused by: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Unconvertable Object of class: java.util.ArrayList
at grails.converters.JSON.value(JSON.java:197)
at grails.converters.JSON.render(JSON.java:134)
... 2 more

The controller example below appears to work but I am getting the error above in services and other classes. I tried using mockCodec with no luck. Am I doing this incorrectly? Or if this is an issue, is there a way to work around this? Thank you.
@TestFor(JustatestService)
class JustatestServiceTests {

    void setUp(){
       // mockCodec(JSONCodec)
    }

    void testSomething() {
        //mockCodec(JSONCodec)
        println service.serviceMethod()
    }
}

class JustatestService {

    def serviceMethod() {
        def input = ["one","two"]
        def test = ["one","two"] as JSON
        println test

        def first = new JSON(input)
        println first
    }

}

class SimpleController {
    def index() {
        def input = ["one","two"]
        def first = new JSON(input)
        def two = input as JSON
        println first
        println two
    }
}

@TestFor(SimpleController)
class SimpleControllerTests {
    void testSomething() {
       controller.index()
    }
}

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

Re: Error using as JSON converter in a service in a unit test

Graeme Rocher
Administrator
The default unit test mixin for services doesn't setup converters, you may need to add @TestMixin(ControllerUnitTestMixin) to your test as well


On Tue, Nov 6, 2012 at 7:17 PM, Brandon Fish <[hidden email]> wrote:
Hello, 

I am getting the following error when trying to serialize a list to JSON using as JSON in a class:

| Failure:  testSomething(jsoningroovy.JustatestServiceTests)
|  org.apache.commons.lang.UnhandledException: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Unconvertable Object of class: java.util.ArrayList
at jsoningroovy.JustatestService.serviceMethod(JustatestService.groovy:13)
at jsoningroovy.JustatestServiceTests.testSomething(JustatestServiceTests.groovy:20)
Caused by: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Unconvertable Object of class: java.util.ArrayList
at grails.converters.JSON.value(JSON.java:197)
at grails.converters.JSON.render(JSON.java:134)
... 2 more

The controller example below appears to work but I am getting the error above in services and other classes. I tried using mockCodec with no luck. Am I doing this incorrectly? Or if this is an issue, is there a way to work around this? Thank you.
@TestFor(JustatestService)
class JustatestServiceTests {

    void setUp(){
       // mockCodec(JSONCodec)
    }

    void testSomething() {
        //mockCodec(JSONCodec)
        println service.serviceMethod()
    }
}

class JustatestService {

    def serviceMethod() {
        def input = ["one","two"]
        def test = ["one","two"] as JSON
        println test

        def first = new JSON(input)
        println first
    }

}

class SimpleController {
    def index() {
        def input = ["one","two"]
        def first = new JSON(input)
        def two = input as JSON
        println first
        println two
    }
}

@TestFor(SimpleController)
class SimpleControllerTests {
    void testSomething() {
       controller.index()
    }
}




--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error using as JSON converter in a service in a unit test

bjfish
I was not able to get this to work with @TestMixin(ControllerUnitTestMixin) but adding @TestFor(SimpleController) to my service test did work.

Thank you for the response

On Wed, Nov 7, 2012 at 2:16 AM, Graeme Rocher <[hidden email]> wrote:
The default unit test mixin for services doesn't setup converters, you may need to add @TestMixin(ControllerUnitTestMixin) to your test as well


On Tue, Nov 6, 2012 at 7:17 PM, Brandon Fish <[hidden email]> wrote:
Hello, 

I am getting the following error when trying to serialize a list to JSON using as JSON in a class:

| Failure:  testSomething(jsoningroovy.JustatestServiceTests)
|  org.apache.commons.lang.UnhandledException: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Unconvertable Object of class: java.util.ArrayList
at jsoningroovy.JustatestService.serviceMethod(JustatestService.groovy:13)
at jsoningroovy.JustatestServiceTests.testSomething(JustatestServiceTests.groovy:20)
Caused by: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Unconvertable Object of class: java.util.ArrayList
at grails.converters.JSON.value(JSON.java:197)
at grails.converters.JSON.render(JSON.java:134)
... 2 more

The controller example below appears to work but I am getting the error above in services and other classes. I tried using mockCodec with no luck. Am I doing this incorrectly? Or if this is an issue, is there a way to work around this? Thank you.
@TestFor(JustatestService)
class JustatestServiceTests {

    void setUp(){
       // mockCodec(JSONCodec)
    }

    void testSomething() {
        //mockCodec(JSONCodec)
        println service.serviceMethod()
    }
}

class JustatestService {

    def serviceMethod() {
        def input = ["one","two"]
        def test = ["one","two"] as JSON
        println test

        def first = new JSON(input)
        println first
    }

}

class SimpleController {
    def index() {
        def input = ["one","two"]
        def first = new JSON(input)
        def two = input as JSON
        println first
        println two
    }
}

@TestFor(SimpleController)
class SimpleControllerTests {
    void testSomething() {
       controller.index()
    }
}




--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

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

Re: Error using as JSON converter in a service in a unit test

zyro
confirmed, using @TestFor(SomeController) makes JSON converter work. But using @TestFor with some dummy controller to make the converters work in a test for something else looks wrong.

what does @TestFor do in addition to @TestMixin(ControllerUnitTestMixin) that may impact the converters?

thanks,

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

Re: Error using as JSON converter in a service in a unit test

zyro
bump...

i hit this one today as well in a @TestFor(MyService) annotated unit test.

adding @TestMixin(ControllerUnitTestMixin) does not load the coverters
it seems...

ideas?

zyro

-------- Original Message  --------
Subject: [grails-user] Re: Error using as JSON converter in a service in
a unit test
From: zyro <[hidden email]>
To: [hidden email]
Date: Mon, 26 Nov 2012 05:51:53 -0800 (pst)

> confirmed, using @TestFor(SomeController) makes JSON converter work. But
> using @TestFor with some dummy controller to make the converters work in a
> test for something else looks wrong.
>
> what does @TestFor do in addition to @TestMixin(ControllerUnitTestMixin)
> that may impact the converters?
>
> thanks,
>
> zyro
>
>
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Error-using-as-JSON-converter-in-a-service-in-a-unit-test-tp4637433p4638075.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


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

Re: Error using as JSON converter in a service in a unit test

Denny Wordtwo
Got this error today myself.

Writing a Test for a Service and adding @TestMixin(ControllerUnitTestMixin) has not solved the problem.


Ideas?

My current Grails Version is 2.1.1.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error using as JSON converter in a service in a unit test

Denny Wordtwo
I started the Test within Eclipse as Unit Test and did not work.
Now I started it with 'test-app' and it worked.
Loading...