简体   繁体   中英

JSON validation using REST assured

i'm trying to validate Json Objects. I use https://code.google.com/p/rest-assured/wiki/Downloads?tm=2 ,

import static com.jayway.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class testClass {


    @Test public void
    validates_schema_in_classpath() {
        // Given
        String JsonString = "{\"isSuccess\":false}";// Greeting response

        // Then
        assertThat(JsonString, matchesJsonSchemaInClasspath("greeter-schema.json"));
    }
}

greeter-schema.json: http://cs606926.vk.me/v606926718/15603/0Kauo1bTDi8.jpg

I have OK result everytime even if JsonString is not equal this "{\\"isSuccess\\":false}".

For example I get OK result when JsonString="{\\"isSuccess\\":false},{\\"isFalse\\":true}", or "{\\"isSuccess\\":false},testetstets"

Schema validation using RestAssured only asserts that the value is there. To assert against a certain value, you have to denote exactly what name (isSuccess in this case) you are looking for to get the value. Then validate against this derived value:

assertThat().body("isSuccess", equalTo(false));

This functionality is actually what RestAssured was built to do, and there is plenty of info on it here: https://github.com/jayway/rest-assured/wiki/Usage#json-using-jsonpath

Your JSON schema only checks that the attribute "isSuccess" is present, not that it must be either true or false. You can do this with json schema as well but I don't know it by heart. You can probably just google for it check out http://json-schema.org .

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