【问题标题】:MockMVC jUnit Spring testMockMVC jUnit Spring 测试
【发布时间】:2016-11-30 04:34:57
【问题描述】:

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

【问题讨论】:

    标签: java spring junit mockmvc


    【解决方案1】:

    尝试使用 hamcrest 匹配器:

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

    【讨论】:

    • 非常感谢,它有效。记录其 org.hamcrest.Matchers
    猜你喜欢
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2019-07-30
    • 2018-10-22
    • 2013-12-28
    • 1970-01-01
    相关资源
    最近更新 更多