gpt4 book ai didi

java - MockMVC jUnit Spring 测试

转载 作者:行者123 更新时间:2023-11-30 07:14:50 24 4
gpt4 key购买 nike

我需要 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com