我正在尝试进行测试,检查某个列表是否有项目,我不关心订单 我希望能够做到这一点的方法是测试项目是否具有某个具有特定值的属性。
我用以下代码隔离了senario:
我正在使用的课程:
public class A {
private String propA;
public A (final String propA) {
this.propA = propA;
}
public String getPropA() {
return propA;
}
public void setPropA(final String propA) {
this.propA = propA;
}
}
识别TestClass
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class HamcrestCollectionTest {
@Test
public void testContainsInAnyOrder() {
List<A> list = new ArrayList<A>();
list.add(new A("a"));
list.add(new A("b"));
assertThat(list, containsInAnyOrder(hasProperty("propA", equalTo("b")), hasProperty("propA", equalTo("a"))));
}
}
此测试失败。如果我在countainsInAnyOrder中切换列表的值,那么这是有效的。这并不是我期望的“containsInAnyOrder”。
这样做的正确方法是什么?
或者有没有办法检查各个值是否存在?
答案 0 :(得分:5)
我发现了问题所在。它确实是一个引起问题的Hamcrest类的版本。
采取的步骤:
所以基本上是使用 mockito-all 引起的冲突。