【问题标题】:Using hamcrest for comparing each item in two separate lists with own matcher使用 hamcrest 将两个单独列表中的每个项目与自己的匹配器进行比较
【发布时间】:2010-08-23 15:00:47
【问题描述】:

我尝试比较两个列表:

ListA (a1,a2,a3,...)
ListB (b1,b2,b3,...)

我希望将 a1 与 b1、a2 与 b2、a3 与 b3 进行比较,......

但我必须使用另一种方法,不能使用 .equals!

我已经编写了自己的 hamcrest matcher。但我必须使用 for 循环来迭代元素。有没有更好的解决方案?

for(int i = 0;i<expected.size();i++){
   assertThat(item.get(i),equalsModel(expected.get(0)));
}

【问题讨论】:

    标签: java collections hamcrest


    【解决方案1】:

    改用迭代器怎么样?

    for(
        Iterator<String> it1 = list1.iterator(), it2 = list2.iterator();
        it1.hasNext() && it2.hasNext();
    ){
        assertThat(it1.next(),equalsModel(it2.next()));
    }
    

    【讨论】:

    • 嗯,朝着那个方向前进。我想要这样的东西:assertThat(list1,equalsToList(list2).using(selkowsTreeDistance()));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多