hrs
hrs

Reputation: 1

restAssured json schema validation - reading json schema file assertion getting failed

I am using restAssured for my json schema validation. Below is my script for assertion: String JsonString = response.asString(); Assert.assertEquals(JsonString,matchesJsonSchemaInClasspath("quotesschema.json"));

And I have placed my quotesschema.json file in project/bin folder.

When I run my test script, assertion fails with below message java.lang.AssertionError: expected [] but found [actual api response]

Also, I validated my schema against api response thru http://json-schema-validator.herokuapp.com/ schema validator.

Not sure whether its reading my schema inside the .son file. Below is my schema.

{
    "type": "object",
    "$schema": "http://json-schema.org/draft-03/schema",
    "title": "quotes-schema",
    "description": "JSON Schema for Quotes",
        "properties": {
            "result": {
                "type": "object",
                "properties": {
                    "Quotes": {
                        "type": "array",
                            "properties": {
                                "DisplaySymbol": {
                                    "type": "string"
                                    },
                                "Identifier": {
                                    "type": "string"                                    
                                    },
                                "Exchange": {
                                    "type": "string"
                                    },
                                "Trade": {
                                    "type": "string"
                                    },
                                "Date": {
                                    "type": "string"
                                    },
                                "Change": {
                                    "type": "string"
                                    },
                                "Bid": {
                                    "type": "string"
                                    },
                                "BidSize": {
                                    "type": "string"
                                    },
                                "Ask": {
                                    "type": "string"
                                    },
                                "AskSize": {
                                    "type": "string"
                                    },
                                "High": {
                                    "type": "string"
                                    },
                                "Low": {
                                    "type": "string"
                                    },
                                "Volume": {
                                    "type": "string"
                                    },
                                "Open": {
                                    "type": "string"
                                    },
                                "PreviousClose": {
                                    "type": "string"
                                    },
                                "High52Week": {
                                    "type": "string"
                                    },
                                "High52WeekDate": {
                                    "type": "string"
                                    },
                                "Low52Week": {
                                    "type": "string"
                                    },
                                "Low52WeekDate": {
                                    "type": "string"
                                    },
                                "PERatio": {
                                    "type": "string"
                                    },
                                "MarketCap": {
                                    "type": "string"
                                    },
                                "SharesOutstanding": {
                                    "type": "string"
                                    },
                                "RollingEPS": {
                                    "type": "string"
                                    },
                                "IsDefault": {
                                    "type": "string"
                                    },
                                "IsIndex": {
                                    "type": "string"
                                    },
                                "Class": {
                                    "type": "string"
                                    }
                            }
                        }
                    }
                }
}
}

Upvotes: 0

Views: 2430

Answers (1)

parishodak
parishodak

Reputation: 4676

matchesJsonSchemaInClasspath method returns JsonSchemaValidator class and not a String.

That's why your Assert will not work, where strings are compared:

JsonString = response.asString();
Assert.assertEquals(JsonString,matchesJsonSchemaInClasspath("quotesschema.json"));   

Actual usage be inside body() method as explained below:

get("RESTPATH").then().assertThat().body(matchesJsonSchemaInClasspath("quotesschema.json"));

Upvotes: 2

Related Questions