Using JUnit with JacksonRepresentation

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

Using JUnit with JacksonRepresentation

alexh
Hi,

I'd like to be able to write some tests using JUnit to rest server resource classes. I'm encountering a problem when I try to pass a JacksonRepresentation in. My test class looks something a little like this:

{
        TransactionResource resource = new MyResource();
        MyData data = new MyData(etc);

        Representation entity = new JacksonRepresentation<MyData>(myData);

        entity.setMediaType(MediaType.APPLICATION_JSON);
       
        Representation response = resource.post(entity);
}

This is all fine as far as I know. But I receive an error in my ServerResource derived class when I try and deserialize the object using Jackson:

            MyData myData = new JacksonRepresentation<>(entity, MyData.class)
                    .getObject();

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: UNKNOWN; line: 1, column: 1]
        at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
        at com.fasterxml.jackson.databind.ObjectReader._initForReading(ObjectReader.java:1298)
        at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1199)
        at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:798)
        at org.restlet.ext.jackson.JacksonRepresentation.getObject(JacksonRepresentation.java:309)

Is this the correct approach? Am I doing something wrong in my test case in setting up the call to my resource class?

Thanks

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3086450
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using JUnit with JacksonRepresentation

Tim Peierls
Hard to be sure from what you've described, but it looks as though the entity doesn't contain anything. Try getting the raw contents of the entity in two places (1) before you post it and and (2) after you receive it but before you parse it by passing it to the JacksonRepresentation constructor. Does it look like valid JSON in both cases? 

--tim


On Fri, Aug 8, 2014 at 5:13 PM, Alex Harvey <[hidden email]> wrote:
Hi,

I'd like to be able to write some tests using JUnit to rest server resource classes. I'm encountering a problem when I try to pass a JacksonRepresentation in. My test class looks something a little like this:

{
        TransactionResource resource = new MyResource();
        MyData data = new MyData(etc);

        Representation entity = new JacksonRepresentation<MyData>(myData);

        entity.setMediaType(MediaType.APPLICATION_JSON);

        Representation response = resource.post(entity);
}

This is all fine as far as I know. But I receive an error in my ServerResource derived class when I try and deserialize the object using Jackson:

            MyData myData = new JacksonRepresentation<>(entity, MyData.class)
                    .getObject();

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: UNKNOWN; line: 1, column: 1]
        at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
        at com.fasterxml.jackson.databind.ObjectReader._initForReading(ObjectReader.java:1298)
        at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1199)
        at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:798)
        at org.restlet.ext.jackson.JacksonRepresentation.getObject(JacksonRepresentation.java:309)

Is this the correct approach? Am I doing something wrong in my test case in setting up the call to my resource class?

Thanks

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3086450

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

RE: Re: Using JUnit with JacksonRepresentation

alexh
Hi,

Thanks for the reply. Yes I tried calling Representation.getTest() on either side of the call and the JSON appears to be correctly formed.

I did notice the following message in my output when running under JUnit.

WARNING: The GAE edition is unable to get an InputStream out of an OutputRepresentation.

This is a GAE application. Could this be the problem?

Thanks,

Alex

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3086557
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Using JUnit with JacksonRepresentation

Tim Peierls
Sure sounds like that's the problem. For unit testing purposes, how about getting the text of representation, turning that into a StringRepresentation, and then constructing the JacksonRepresentation in terms of the StringRepresentation? It's wasteful, but it would allow you to proceed with your unit testing.

--tim


On Sun, Aug 10, 2014 at 4:48 PM, Alex Harvey <[hidden email]> wrote:
Hi,

Thanks for the reply. Yes I tried calling Representation.getTest() on either side of the call and the JSON appears to be correctly formed.

I did notice the following message in my output when running under JUnit.

WARNING: The GAE edition is unable to get an InputStream out of an OutputRepresentation.

This is a GAE application. Could this be the problem?

Thanks,

Alex

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3086557

Loading...