gpt4 book ai didi

spring - Spring MockMvc验证正文为空

转载 作者:行者123 更新时间:2023-12-04 01:37:35 24 4
gpt4 key购买 nike

我有一个简单的 Spring 测试

@Test
public void getAllUsers_AsPublic() throws Exception {
doGet("/api/users").andExpect(status().isForbidden());
}

public ResultActions doGet(String url) throws Exception {
return mockMvc.perform(get(url).header(header[0],header[1])).andDo(print());
}

我想确认响应主体为空。例如。做类似 .andExpect(content().isEmpty())的事情

最佳答案

有一种更清洁的方法:

andExpect(jsonPath("$").doesNotExist())

请注意,您不能使用 isEmpty,因为它会检查一个空值,并假定该属性存在。当属性不存在时, isEmpty引发异常。而 doesNotExist验证该属性不存在,并且与 $一起使用时,它会检查空的JSON文档。

关于spring - Spring MockMvc验证正文为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46420404/

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