我寫了Spring控制器Junits。 我使用JsonPath使用["$..id"]
從JSON中獲取所有ID。用Hamcrest Matchers檢查JsonPath的輸出
我有以下的測試方法:
mockMvc.perform(get(baseURL + "/{Id}/info", ID).session(session))
.andExpect(status().isOk()) // Success
.andExpect(jsonPath("$..id").isArray()) // Success
.andExpect(jsonPath("$..id", Matchers.arrayContainingInAnyOrder(ar))) // Failed
.andExpect(jsonPath("$", Matchers.hasSize(ar.size()))); // Success
以下是我傳遞的數據: -
List<String> ar = new ArrayList<String>();
ar.add("ID1");
ar.add("ID2");
ar.add("ID3");
ar.add("ID4");
ar.add("ID5");
我失敗消息爲: -
Expected: [<[ID1,ID2,ID3,ID4,ID5]>] in any order
but: was a net.minidev.json.JSONArray (<["ID1","ID2","ID3","ID4","ID5"]>)
問題是:如何處理JSONArray與org.hamcrest.Matchers;
有沒有簡單的方法可以使用jsonPath。
設置: - hamcrest-all-1.3 jar
,json-path-0.9.0.jar
,spring-test-4.0.9.jar