java 断言响应正文是空列表,放心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30460682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:10:20  来源:igfitidea点击:

Assert that response body is empty list with rest-assured

javarest-assured

提问by atamanroman

How can I check with rest-assured (2.4.0) if the response json is an empty list?

如果响应 json 是空列表,我如何检查 rest-assured (2.4.0)?

Given the response [](with header content-type=application/json) I tried:

鉴于响应[](带有 header content-type=application/json),我尝试了:

.body(Matchers.emptyArray()) // expected: an empty array, actual: []
.body("/", Matchers.emptyArray()) // invalid expression /
.body(".", Matchers.emptyArray()) // invalid expression .

回答by Johan

The problem is (probably) that REST Assured returns a List and not an array (and Hamcrest differentiate between the two). You can do:

问题是(可能)REST Assured 返回一个列表而不是一个数组(Hamcrest 区分两者)。你可以做:

.body("", Matchers.hasSize(0))

or

或者

.body("$", Matchers.hasSize(0))

or

或者

.body("isEmpty()", Matchers.is(true))

回答by kazimierz

Inspired by what @Johan said I tried this and I think it tells more to reader than other suggestions.

受到@Johan 所说的启发,我尝试了这个,我认为它比其他建议更能告诉读者。

.body("", equalTo(Collections.emptyList()))