ActiveWeb: testing JSON response attributes and values

58 views Asked by At

What is the preferred way to test JSON response, especially the attributes and values to be present in ? What is the right type of a controller to extend from: IntegrationSpec, ControllerSpec, AppIntegrationSpec? I didn't find the proper response in the corresponding Testing section. Thank you.

1

There are 1 answers

3
ipolevoy On

There is a good example if this in this sample project: https://github.com/javalite/activeweb-rest/blob/27687e26f5476734b867d01f38b2575c6de3f3c9/src/test/java/app/controllers/PeopleControllerSpec.java#L57

public void shouldCreateNewPeopleWithAddresses(){

        String json = Util.readResource("/people.json");
        request().content(json.getBytes()).contentType("application/json").post("create"); // <--- need to call "create" according to REST routing

        Map response = JsonHelper.toMap(responseContent());

        the(response.get("code")).shouldBeEqual(200);
        the(response.get("message")).shouldBeEqual("successfully created people and addresses");
}

Basically, you get a responseContent() as String, then you can do whatever you want with it. The super class for the spec is irrelevant.