Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 断言该集合;至少包含一个非空元素";_Java_Unit Testing_Junit_Assertions_Hamcrest - Fatal编程技术网

Java 断言该集合;至少包含一个非空元素";

Java 断言该集合;至少包含一个非空元素";,java,unit-testing,junit,assertions,hamcrest,Java,Unit Testing,Junit,Assertions,Hamcrest,我想验证集合是否至少包含一个非null元素。我尝试了is(不是(empty()),但是在下面的测试中它通过了 import org.junit.Test; import java.util.ArrayList; import java.util.Collection; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static or

我想验证集合是否至少包含一个非null元素。我尝试了
is(不是(empty())
,但是在下面的测试中它通过了

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.not;

public class SandBoxTest {
    @Test
    public void shouldTestThis() {
        Collection<Integer> collection = new ArrayList<Integer>();
        collection.add(null);

        assertThat(collection, is(not(empty())));
    }
}
import org.junit.Test;
导入java.util.ArrayList;
导入java.util.Collection;
导入静态org.hamcrest.CoreMatchers.is;
导入静态org.hamcrest.matcherasert.assertThat;
导入静态org.hamcrest.Matchers.empty;
导入静态org.hamcrest.Matchers.not;
公共类沙盒测试{
@试验
public void应测试此(){
集合集合=新的ArrayList();
collection.add(空);
断言(集合,不是(空的()));
}
}
有没有一种优雅/简单的方法可以做到这一点

不起作用的事情

@Test
public void should(){
    Collection<String> collection = new ArrayList();
    collection.add("gfas");
    collection.add("asda");
    assertThat(collection, contains(notNullValue()));
}

java.lang.AssertionError: 
Expected: iterable containing [not null]
     but: Not matched: "asda"
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
@测试
公共空间应该(){
集合集合=新的ArrayList();
收集。添加(“gfas”);
收款。添加(“asda”);
assertThat(集合,包含(notNullValue());
}
java.lang.AssertionError:
应为:iterable包含[非空]
但是:不匹配:“asda”
位于org.hamcrest.matcherasert.assertThat(matcherasert.java:20)

您在正确的轨道上,链接
Matcher
实例。您只需要
hasItem
匹配器(就像我建议的那样),而不需要
包含

Iterable
s创建匹配器,该匹配器仅在单次传递时匹配 在检查的
Iterable
中,至少有一个项目与 指定的
项匹配器
。在匹配时,遍历 已检查的
Iterable
将在找到匹配项后立即停止

比如说,

Collection<Integer> collection = new ArrayList<Integer>();
collection.add(null);

assertThat(collection, hasItem(is(not(nullValue()))));
鉴于

Collection<Integer> collection = new ArrayList<Integer>();
collection.add(null);
collection.add("hey");
collection.add(null);

assertThat(collection, hasItem(is(not(nullValue()))));
// or shortened
assertThat(collection, hasItem(notNullValue()));
Collection=newarraylist();
collection.add(空);
收藏。添加(“嘿”);
collection.add(空);
assertThat(集合,hasItem(不是(nullValue())));
//或缩短
资产(集合,hasItem(notNullValue());

将成功。

您可以尝试以下操作:

public void shouldTestThis() {
        Collection<Integer> collection = new ArrayList<Integer>();
        collection.add(null);
        collection.removeAll(Collections.singleton(null)); // remove all "null" elements from collection
        assertThat(collection, is(not(empty())));
    }
public void shouldTestThis(){
集合集合=新的ArrayList();
collection.add(空);
collection.removeAll(Collections.singleton(null));//从集合中删除所有“null”元素
断言(集合,不是(空的()));
}
正如ajb所注意到的,如果希望不修改数组,则应该使用迭代器并检查每个元素,直到集合结束或非空元素为止

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

...

assertThat(collection, hasItem(notNullValue(Integer.class)));
不幸的是,如果您使用的是1.6,则可能需要将其拆分为两行:

Matcher<Iterable<? super String>> matcher = hasItem(notNullValue(Integer.class));
assertThat(collection, matcher);

FEST只需要一个静态导入,因此您可以获得完整的IDE自动完成。

我遇到了同样的问题,并按如下方式解决了它

基本思想是,如果集合只有
null
元素,则转换为一个集合后,它将只包含一个元素,并且它将是
null
。如果不是这样,则集合至少包含一个非null元素

我编写了一个matcher,并通过以下测试进行了尝试:

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static personal.CollectionOfNullsMatcher.collectionOfNulls;


public class SimpleTest {

    @Test
    public void should_check_collection_for_non_null_values() {
        Collection<String> testedCollection = new ArrayList<String>();
        testedCollection.add(null);

        assertThat(testedCollection, is(collectionOfNulls()));

        testedCollection.add("any");

        assertThat(testedCollection, is(not(collectionOfNulls())));
    }
}

class CollectionOfNullsMatcher extends TypeSafeMatcher<Collection> {

    @Override
    protected boolean matchesSafely(final Collection collection) {
        Set<Object> set = new HashSet<Object>(collection);
        return (set.size() == 1) && (set.toArray()[0] == null);
    }

    @Override
    public void describeTo(final Description description) {
        description.appendText("collection of nulls");
    }

    @Factory
    public static <T> Matcher<Collection> collectionOfNulls() {
        return new CollectionOfNullsMatcher();
    }
}
import org.hamcrest.Description;
导入org.hamcrest.Factory;
导入org.hamcrest.Matcher;
导入org.hamcrest.TypeSafeMatcher;
导入org.junit.Test;
导入java.util.ArrayList;
导入java.util.Collection;
导入java.util.HashSet;
导入java.util.Set;
导入静态org.hamcrest.CoreMatchers.is;
导入静态org.hamcrest.CoreMatchers.not;
导入静态org.junit.Assert.assertThat;
导入静态personal.CollectionOfNullsMatcher.collectionOfNulls;
公共类SimpleTest{
@试验
public void应检查集合中的非空值(){
Collection testedCollection=new ArrayList();
testedCollection.add(空);
资产(testedCollection,是(collectionOfNulls());
测试收集。添加(“任何”);
资产(testedCollection,不是(collectionOfNulls()));
}
}
类CollectionOfNullsMatcher扩展了TypeSafeMatcher{
@凌驾
受保护的布尔匹配安全(最终集合){
Set=新哈希集(集合);
返回(set.size()==1)和&(set.toArray()[0]==null);
}
@凌驾
公共无效说明(最终说明){
description.appendText(“空值集合”);
}
@工厂
公共静态匹配器集合fnulls(){
返回新集合FnullsMatcher();
}
}
当然,在实际项目中,matcher应该与其兄弟放在一起:)


希望有帮助。

重点是检查它是否有
null
元素。您不应该删除它们。重点是“验证集合是否至少包含一个非空元素”。您可以通过制作集合的副本(声明临时集合
c
并使用
c.addAll
)来实现这一点,而无需删除
null
元素。但是对于这样的工作量,您也可以只使用迭代器。我可能会使用这种方法,但是如果集合是不可变的,那么这种方法就不起作用。如果集合是不可变的,您就必须使用迭代器,如
@ajb
noticesNice,一个可重用的匹配器。
import static org.fest.assertions.api.Assertions.assertThat;
...
assertThat(collection).doesNotContainNull();
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static personal.CollectionOfNullsMatcher.collectionOfNulls;


public class SimpleTest {

    @Test
    public void should_check_collection_for_non_null_values() {
        Collection<String> testedCollection = new ArrayList<String>();
        testedCollection.add(null);

        assertThat(testedCollection, is(collectionOfNulls()));

        testedCollection.add("any");

        assertThat(testedCollection, is(not(collectionOfNulls())));
    }
}

class CollectionOfNullsMatcher extends TypeSafeMatcher<Collection> {

    @Override
    protected boolean matchesSafely(final Collection collection) {
        Set<Object> set = new HashSet<Object>(collection);
        return (set.size() == 1) && (set.toArray()[0] == null);
    }

    @Override
    public void describeTo(final Description description) {
        description.appendText("collection of nulls");
    }

    @Factory
    public static <T> Matcher<Collection> collectionOfNulls() {
        return new CollectionOfNullsMatcher();
    }
}