Java 使用assertThat测试列表列表是否包含特定元素
我尝试使用Java 使用assertThat测试列表列表是否包含特定元素,java,junit4,assert,matcher,hamcrest,Java,Junit4,Assert,Matcher,Hamcrest,我尝试使用Assert\assertThat检查列表列表中的每个列表是否包含特定值 我已经写了这段代码,但它不起作用。 测试未编译,代码用红线下划线,并显示错误消息(查看注释): 渐变依赖项: testCompile group: 'junit', name: 'junit', version: '4.12' 我希望这有帮助,我尝试了另一种方法来搜索元素。我使用嵌套的foreach循环查看列表是否包含该特定元素 List<List<String>> tested
Assert\assertThat
检查列表列表中的每个列表是否包含特定值
我已经写了这段代码,但它不起作用。
测试未编译,代码用红线下划线,并显示错误消息(查看注释):
渐变依赖项:
testCompile group: 'junit', name: 'junit', version: '4.12'
我希望这有帮助,我尝试了另一种方法来搜索元素。我使用嵌套的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);`
List testedList=new ArrayList();
布尔存在=假;
List firstList=新建ArrayList();
第一列表。添加(“1”);
第一列表。添加(“2”);
List secondList=new ArrayList();
第二名单。添加(“2”);
第二名单。添加(“3”);
testedList.add(第一个列表);
testedList.add(第二个列表);
对于(列表e:测试列表){
用于(字符串i:e){
如果(i.同等信号情况(“2”))
存在=真实;
}
}System.out.println(存在)`
我认为每个项目
匹配器都不是设计用来与hasItem
一起工作的,你会发现自己掉进泛型地狱
除了定义一个将“封装”泛型限制并委托给hasItem
matcher的匹配器之外,我看不到一个简单的解决方案。幸运的是,定义一个新的匹配器确实是一项简单的任务。看看:
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);
}
}
私有类HasItemMatcher扩展了BaseMatcher{
private final Matcher你说它不工作是什么意思?第一个断言对我来说运行正常,它显示错误消息,如我在Comments中所述。错误是否发生在“everyItem”方法上?@PabloB它未编译。错误消息显示在所有assert#assertThat内容上。我不想在测试中用于循环。
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
import java.util.ArrayList;
testCompile group: 'junit', name: 'junit', version: '4.12'
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);`
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")));
}