2016-07-26 161 views
1

我需要幫助jUnit測試,我的結果是json文件,而且我需要測試該文件的長度,問題是jsonPath函數裏面沒有這樣的方法。MockMVC jUnit Spring測試

@Test 
@Transactional 
    public void getMunicipalitiesByCity() throws Exception { 

     restSampleMockMvc.perform(get("/api/insurancePlan/municipalities/city/{cityName}", "esc")) 
       .andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON)) 
       .andExpect(jsonPath("$.length").value(7)); 

這裏是我的測試方法,預計長度爲7 任何幫助將感激,感謝

回答

1

嘗試使用hamcrest匹配:

.andExpect((jsonPath("$", Matchers.hasSize(7)))); 
+0

非常感謝你,它的工作原理。 備案其org.hamcrest.Matchers –