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"

2
  • word of advice: paste the schema as code in the question instead of linking to external resource Commented Apr 21, 2014 at 16:27
  • I have no reputation to do this - schema contains a lot of urls Commented Apr 21, 2014 at 16:52

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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
Why don't you look at the web page at the site that I provided to you? The documentation is available at json-schema.org/latest/json-schema-validation.html. See section 5.3 for info on arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.