Java MockMVC jUnit Spring测试

Java MockMVC jUnit Spring测试,java,spring,junit,mockmvc,Java,Spring,Junit,Mockmvc,我需要jUnit测试的帮助,我的结果是json文件,我需要测试该文件的长度,问题是jsonPath函数中没有这样的方法 @Test @Transactional public void getMunicipalitiesByCity() throws Exception { restSampleMockMvc.perform(get("/api/insurancePlan/municipalities/city/{cityName}", "esc"))

我需要jUnit测试的帮助,我的结果是json文件,我需要测试该文件的长度,问题是jsonPath函数中没有这样的方法

@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。
任何帮助都将不胜感激,谢谢

尝试使用hamcrest matcher:

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

非常感谢,它很有效。为了记录在案,它的org.hamcrest.Matchers