java 是否有 Hamcrest 匹配器来检查 Collection 既不为空也不为空?

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

Is there a Hamcrest matcher to check that a Collection is neither empty nor null?

javajunitmatcherhamcrest

提问by jhyot

Is there a Hamcrest matcher which checks that the argument is neither an empty Collection nor null?

是否有 Hamcrest 匹配器检查参数既不是空集合也不是空?

I guess I could always use

我想我总是可以使用

both(notNullValue()).and(not(hasSize(0))

but I was wondering whether there is a simpler way and I missed it.

但我想知道是否有更简单的方法,我错过了。

采纳答案by eee

You can combine the IsCollectionWithSizeand the OrderingComparisonmatcher:

您可以结合使用IsCollectionWithSizeOrderingComparison匹配器:

@Test
public void test() throws Exception {
    Collection<String> collection = ...;
    assertThat(collection, hasSize(greaterThan(0)));
}
  • For collection = nullyou get

    java.lang.AssertionError: 
    Expected: a collection with size a value greater than <0>
        but: was null
    
  • For collection = Collections.emptyList()you get

    java.lang.AssertionError: 
    Expected: a collection with size a value greater than <0>
        but: collection size <0> was equal to <0>
    
  • For collection = Collections.singletonList("Hello world")the test passes.
  • collection = null你得到

    java.lang.AssertionError: 
    Expected: a collection with size a value greater than <0>
        but: was null
    
  • collection = Collections.emptyList()你得到

    java.lang.AssertionError: 
    Expected: a collection with size a value greater than <0>
        but: collection size <0> was equal to <0>
    
  • 对于collection = Collections.singletonList("Hello world")测试通过。


Edit:

编辑:

Just noticed that the following approch is notworking:

只注意到下面的计算策略是工作:

assertThat(collection, is(not(empty())));

The more i think about it the more i would recommend a slightly altered version of the statement written by the OP if you want to test explicitly for null.

如果您想明确测试是否为空,我越想越推荐 OP 编写的语句的稍微更改版本。

assertThat(collection, both(not(empty())).and(notNullValue()));

回答by mike

As I posted in the comments, the logical equivalent of collection != nulland size != 0is size > 0, that implies the collection is not null. A simpler way to express size > 0is there is an (arbitrary) element X in collection. Below a working code example.

正如我在评论中发布的那样,collection != nulland的逻辑等价物size != 0size > 0,这意味着该集合不为空。更简单的表达方式size > 0there is an (arbitrary) element X in collection。下面是一个工作代码示例。

import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.hamcrest.CoreMatchers.anything;

public class Main {

    public static void main(String[] args) {
        boolean result = hasItem(anything()).matches(null);
        System.out.println(result); // false for null

        result = hasItem(anything()).matches(Arrays.asList());
        System.out.println(result); // false for empty

        result = hasItem(anything()).matches(Arrays.asList(1, 2));
        System.out.println(result); // true for (non-null and) non-empty 
    }
}

回答by Zon

You are welcome to use Matchers:

欢迎您使用匹配器:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;

assertThat(collection, anyOf(nullValue(), empty()));