3

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"

user3510509
  • 33
  • 1
  • 5

2 Answers2

3

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

Zach David
  • 41
  • 5
1

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.

Johan
  • 37,479
  • 32
  • 149
  • 237
  • I know how I can chek type of attribute, but I need to chek content of JSON object. I mean, I need to check that in JSONobject there aren't additional attributes, and i've it done with "additionalProperties": false. But know I have the next problem: how to check that attribute is present in array? "required":true dont hepls. My test OK every time even if array is empty – user3510509 Apr 22 '14 at 08:53
  • Why don't you look at the web page at the site that I provided to you? The documentation is available at http://json-schema.org/latest/json-schema-validation.html. See section 5.3 for info on arrays. – Johan Apr 23 '14 at 05:16