3
votes

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"

2
word of advice: paste the schema as code in the question instead of linking to external resource - soulcheck
I have no reputation to do this - schema contains a lot of urls - user3510509

2 Answers

3
votes

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

1
votes

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.