Java 断言列表不为空 - 有或没有 Hamcrest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21433497/
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-08-13 08:42:12  来源:igfitidea点击:

Assert that the list is not empty - with or without Hamcrest?

javacollectionsjunithamcrest

提问by Prakhar

This is related to Checking that a List is not empty in Hamcrest

这与检查 Hamcrest 中的列表不为空有关

I have a question over that - If we can assert the list is not empty without using Hamcrest and just using JUnit as:

我有一个问题 - 如果我们可以在不使用 Hamcrest 并且只使用 JUnit 的情况下断言列表不为空:

assertFalse(list.isEmpty());

Is using

正在使用

assertThat(list.isEmpty(), is(false));

or

或者

assertThat((Collection)list, is(not(empty())));

Worthwhile?

值得吗?

I'm not able to understand are we gaining something using Hamcrest version in this case? Are both equivalent?

我无法理解在这种情况下我们是否使用 Hamcrest 版本获得了一些东西?两者是等价的吗?

采纳答案by Jeff Storey

They are the same functionality. The hamcrest provides a more English-like readable language and better error messages. In simple cases like this, I would probably just use the assertFalse

它们是相同的功能。hamcrest 提供了更像英语的可读语言和更好的错误消息。在像这样的简单情况下,我可能只使用assertFalse