简体   繁体   中英

ActiveWeb: testing JSON response attributes and values

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM