微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 assertThat 测试列表列表是否包含特定元素

如何解决使用 assertThat 测试列表列表是否包含特定元素

我尝试使用 Assert#assertthat 检查列表列表中的每个列表是否包含特定值。

我已经编写了这段代码,但它不起作用。 测试未编译,代码用红线下划线并显示错误消息(查看注释):

    List<List<String>> testedList = new ArrayList<>();

    List<String> firstList = new ArrayList<>();
    firstList.add("1");
    firstList.add("2");

    List<String> secondList = new ArrayList<>();
    secondList.add("2");
    secondList.add("3");

    testedList.add(firstList);
    testedList.add(secondList);

    Assert.assertthat(testedList,CoreMatchers.everyItem(CoreMatchers.hasItem("2")));
    //          required type       Provided
    // actual:  T                   List<java.util.List<java.lang.String>>
    // matcher: Matcher<? super T>  Matcher<Iterable<Iterable<? super String>>>

即使是显式类型:

    Assert.assertthat(testedList,CoreMatchers.<List<String>>everyItem(CoreMatchers.<String>hasItem("2")));
    // required type: Matcher<List<String>> 
    // Provided:      Matcher<Iterable<? super String>>

如果它只是字符串列表,我可以检查每个字符串是否包含这样的特定字母:

    List<String> testedList = new ArrayList<>();

    testedList.add("1a");
    testedList.add("2a");

    Assert.assertthat(testedList,CoreMatchers.everyItem(CoreMatchers.containsstring("a")));

那么我的代码有什么问题? 我该如何解决

进口:

import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
import java.util.ArrayList;

Gradle 依赖项:

testCompile group: 'junit',name: 'junit',version: '4.12'

解决方法

我认为 everyItem 匹配器不是为与 hasItem 一起使用而设计的,而且您已经发现,您陷入了泛型地狱。

我没有看到一个简单的解决方案,除了定义一个匹配器来“封装”泛型限制并将委托给 hasItem 匹配器。幸运的是,定义一个新的匹配器确实是一项简单的任务。看看:

private class HasItemMatcher<T> extends BaseMatcher<List<T>> {

        private final Matcher<Iterable<? super T>> iterableMatcher;
        public HasItemMatcher(T value) {
            this.iterableMatcher = CoreMatchers.hasItem(value);
        }

        @Override
        public boolean matches(Object item) {
            return iterableMatcher.matches(item);
        }

        @Override
        public void describeTo(Description description) {
            iterableMatcher.describeTo(description);
        }
    }

此代码片段是不言自明的。它将所有工作委托给现有的匹配器。

考虑到这一点,您可以将测试重写为:

    @Test
    public void sampleTest() {
        List<List<String>> testedList = new ArrayList<>();

        List<String> firstList = new ArrayList<>();
        firstList.add("1");
        firstList.add("2");

        List<String> secondList = new ArrayList<>();
        secondList.add("2");
        secondList.add("3");

        testedList.add(firstList);
        testedList.add(secondList);

        Assert.assertThat(testedList,CoreMatchers.everyItem(new HasItemMatcher<>("2")));
    }
,

我希望这会有所帮助,我尝试了一种不同的方式来搜索元素。我使用嵌套的 foreach 循环来查看列表列表是否包含该特定元素。

    List<List<String>> testedList = new ArrayList<>();
    boolean exist = false;

    List<String> firstList = new ArrayList<>();
    firstList.add("1");
    firstList.add("2");

    List<String> secondList = new ArrayList<>();
    secondList.add("2");
    secondList.add("3");

    testedList.add(firstList);
    testedList.add(secondList);

    for(List <String> e : testedList){
        for(String i : e){
        if(i.equalsIgnoreCase("2"))
            exist = true;
        }
    }System.out.println(exist);`

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。