Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 814
user3510509 Avatar asked Sep 16 '26 14:09

user3510509


2 Answers

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

like image 123
Zach David Avatar answered Sep 19 '26 04:09

Zach David


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.

like image 34
Johan Avatar answered Sep 19 '26 02:09

Johan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!