2016-09-14 29 views
2

響應體列表:驗證值返回使用JsonPath

{ 
"Items":[{ 
      "ID": 12, 
      "Name": "n1", 
      "Inactive": false 
     }, 
     { 
      "ID": 16, 
      "Name": "n2", 
      "Inactive": false 
     }, ...etc 
     ] 
} 

使用JsonPath lib下,我能夠用下面的表達來獲得特定元素的所有值(比方說,「無效」)的名單。 '$ .. Inactive'---> [false,false,.. etc]

但是,我不知道如何應用匹配器使用JsonPathAssert斷言上述列表,以便它應該只包含'false 」。

我正在使用Java,JsonPath。有人可以幫助我嗎?

回答

0

經過一番研究,我可以斷言這個列表 - 它應該只包含'false',如下所述。

org.hamcrest.MatcherAssert.assertThat(actualJson.toString(), JsonPathMatchers.hasJsonPath("$..Inactive", Matchers.everyItem(Matchers.equalTo(false))));