2016-01-20 22 views
0

我正在使用restAssured爲我的JSON模式驗證。 以下是我的斷言腳本: String JsonString = response.asString(); Assert.assertEquals(JsonString,matchesJsonSchemaInClasspath(「quotesschema.json」));restAssured json模式驗證 - 讀取json模式文件斷言失敗

而且我已將我的quotesschema.json文件放在項目/ bin文件夾中。

當我運行我的測試腳本,斷言失敗下面的消息 java.lang.AssertionError:預期[],但發現[實際API響應]

另外,我驗證了我對模式通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" 
            } 
          } 
         } 
        } 
       } 
} 
} 

回答

2

matchesJsonSchemaInClasspath方法返回JsonSchemaValidator類,而不是一個String

這就是爲什麼你的斷言將無法正常工作,其中比較字符串:

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

實際使用量在裏面body()方法,說明如下:

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