【问题标题】:restAssured json schema validation - reading json schema file assertion getting failedrestAssured json 模式验证 - 读取 json 模式文件断言失败
【发布时间】:2016-01-20 10:57:08
【问题描述】:

我正在使用 restAssured 进行我的 json 架构验证。 下面是我的断言脚本: 字符串 JsonString = response.asString(); Assert.assertEquals(JsonString,matchesJsonSchemaInClasspath("quotesschema.json"));

我已经把我的quotesschema.json 文件放在了project/bin 文件夹中。

当我运行我的测试脚本时,断言失败并显示以下消息 java.lang.AssertionError: expected [] but found [actual api response]

此外,我通过 http://json-schema-validator.herokuapp.com/ 架构验证器针对 api 响应验证了我的架构。

不确定它是否在 .son 文件中读取我的架构。 下面是我的架构。

{
    "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"
                                    }
                            }
                        }
                    }
                }
}
}

【问题讨论】:

    标签: rest-assured json-schema-validator


    【解决方案1】:

    matchesJsonSchemaInClasspath 方法返回 JsonSchemaValidator 类而不是 String

    这就是你的 Assert 在比较字符串时不起作用的原因:

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

    实际用法在body()方法里面,解释如下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      相关资源
      最近更新 更多