我需要 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 匹配器:
.andExpect((jsonPath("$", Matchers.hasSize(7))));
关于java - MockMVC jUnit Spring 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38591390/