Skip to content

Instantly share code, notes, and snippets.

@belgoros
Last active December 4, 2017 14:50
Show Gist options
  • Save belgoros/838c83439c07fa9d386ce8027fae4cf3 to your computer and use it in GitHub Desktop.
Save belgoros/838c83439c07fa9d386ce8027fae4cf3 to your computer and use it in GitHub Desktop.
JSON response example

I tried to test a single action:

public class ListsController extends APIController  {

    public void index(){
        view("lists", List.findAll().orderBy("id"));
        render().contentType("application/json");
    }
}

public abstract class APIController extends AppController {
    @Override
    protected String getContentType() {
        return "application/json";
    }

    @Override
    protected String getLayout() {
        return null;
    }

}

Here is a test:

public class ListsControllerSpec extends DBControllerSpec {

    @Test
    public void shouldRenderLists(){
        app.models.List list = new app.models.List();
        list.set("id", 1);
        list.set("code", "some code");
        list.set("name", "some name");
        list.set("item_code", "some item code");
        list.saveIt();
        request().integrateViews().get("index");

        the(contentType()).shouldBeEqual("application/json");
        Map[] listsMaps = JsonHelper.toMaps(responseContent());

        the(listsMaps.length).shouldBeEqual(1);
        Map listEntry = listsMaps[0];

        the(listEntry.get("code")).shouldBeEqual("some code");
        the(listEntry.get("name")).shouldBeEqual("some name");
    }
}

and it failed:

[main] INFO FreeMarkerTemplateManager - rendering template: '/lists/index' with layout: 'null', session: 1
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.498 sec <<< FAILURE! - in app.controllers.ListsControllerSpec
shouldRenderLists(app.controllers.ListsControllerSpec)  Time elapsed: 0.411 sec  <<< ERROR!
java.lang.RuntimeException: java.io.EOFException: No content to map to Object due to end of input
	at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2775)
	at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718)
	at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
	at org.javalite.common.JsonHelper.toMaps(JsonHelper.java:50)
	at app.controllers.ListsControllerSpec.shouldRenderLists(ListsControllerSpec.java:22)


Results :

Tests in error: 
  ListsControllerSpec.shouldRenderLists:22 » Runtime java.io.EOFException: No co...

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment