Java Hamcrest 比较集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21624592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hamcrest compare collections
提问by xander27
I'm trying to compare 2 lists:
我正在尝试比较 2 个列表:
assertThat(actual.getList(), is(Matchers.containsInAnyOrder(expectedList)));
But idea
但想法
java: no suitable method found for assertThat(java.util.List<Agent>,org.hamcrest.Matcher<java.lang.Iterable<? extends model.Agents>>)
method org.junit.Assert.<T>assertThat(T,org.hamcrest.Matcher<T>) is not applicable
(no instance(s) of type variable(s) T exist so that argument type org.hamcrest.Matcher<java.lang.Iterable<? extends model.Agents>> conforms to formal parameter type org.hamcrest.Matcher<T>)
method org.junit.Assert.<T>assertThat(java.lang.String,T,org.hamcrest.Matcher<T>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
How should I write it?
我应该怎么写?
采纳答案by Joe
If you want to assert that the two lists are identical, don't complicate things with Hamcrest:
如果你想断言这两个列表是相同的,不要用 Hamcrest 把事情复杂化:
assertEquals(expectedList, actual.getList());
If you really intend to perform an order-insensitive comparison, you can call the containsInAnyOrder
varargs method and provide values directly:
如果您真的打算执行不区分顺序的比较,您可以调用containsInAnyOrder
varargs 方法并直接提供值:
assertThat(actual.getList(), containsInAnyOrder("item1", "item2"));
(Assuming that your list is of String
, rather than Agent
, for this example.)
(对于本例String
,假设您的列表是,而不是Agent
。)
If you really want to call that same method with the contents of a List
:
如果您真的想使用 a 的内容调用相同的方法List
:
assertThat(actual.getList(), containsInAnyOrder(expectedList.toArray(new String[expectedList.size()]));
Without this, you're calling the method with a single argument and creating a Matcher
that expects to match an Iterable
where each elementis a List
. This can't be used to match a List
.
如果没有这个,您将使用单个参数调用该方法并创建一个Matcher
期望匹配 an 的Iterable
地方,其中每个元素都是一个List
。这不能用于匹配List
.
That is, you can't match a List<Agent>
with a Matcher<Iterable<List<Agent>>
, which is what your code is attempting.
也就是说,您不能将 aList<Agent>
与 a匹配Matcher<Iterable<List<Agent>>
,而这正是您的代码所尝试的。
回答by Jofsey
List<Long> actual = Arrays.asList(1L, 2L);
List<Long> expected = Arrays.asList(2L, 1L);
assertThat(actual, containsInAnyOrder(expected.toArray()));
Shorter version of @Joe's answer without redundant parameters.
没有冗余参数的@Joe 答案的较短版本。
回答by Shravan Ramamurthy
To compare two lists with the order preserved use,
要使用保留的顺序比较两个列表,
assertThat(actualList, contains("item1","item2"));
回答by borjab
For list of objects you may need something like this:
对于对象列表,您可能需要这样的东西:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.Matchers.is;
@Test
@SuppressWarnings("unchecked")
public void test_returnsList(){
arrange();
List<MyBean> myList = act();
assertThat(myList , contains(allOf(hasProperty("id", is(7L)),
hasProperty("name", is("testName1")),
hasProperty("description", is("testDesc1"))),
allOf(hasProperty("id", is(11L)),
hasProperty("name", is("testName2")),
hasProperty("description", is("testDesc2")))));
}
Use containsInAnyOrderif you do not want to check the order of the objects.
如果您不想检查对象的顺序,请使用containsInAnyOrder。
P.S. Any help to avoid the warning that is suppresed will be really appreciated.
PS 任何避免被禁止的警告的帮助将不胜感激。
回答by yvolk
With existing Hamcrest libraries (as of v.2.0.0.0) you are forced to use Collection.toArray() method on your Collection in order to use containsInAnyOrder Matcher. Far nicer would be to add this as a separate method to org.hamcrest.Matchers:
使用现有的 Hamcrest 库(从 v.2.0.0.0 开始),您必须在 Collection 上使用 Collection.toArray() 方法才能使用 containsInAnyOrder Matcher。将它作为一个单独的方法添加到 org.hamcrest.Matchers 会更好:
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(Collection<T> items) {
return org.hamcrest.collection.IsIterableContainingInAnyOrder.<T>containsInAnyOrder((T[]) items.toArray());
}
Actually I ended up adding this method to my custom test library and use it to increase readability of my test cases (due to less verbosity).
实际上,我最终将此方法添加到我的自定义测试库中,并使用它来提高我的测试用例的可读性(由于不那么冗长)。
回答by rvazquezglez
To complement @Joe's answer:
补充@Joe的回答:
Hamcrest provides you with three main methods to match a list:
Hamcrest 为您提供了三种主要的方法来匹配列表:
contains
Checks for matching all the elements taking in count the order, if the list has more or less elements, it will fail
contains
检查是否匹配所有按顺序计算的元素,如果列表有更多或更少的元素,它将失败
containsInAnyOrder
Checks for matching all the elements and it doesn't matter the order, if the list has more or less elements, will fail
containsInAnyOrder
检查是否匹配所有元素,顺序无关紧要,如果列表有更多或更少的元素,将失败
hasItems
Checks just for the specified objects it doesn't matter if the list has more
hasItems
只检查指定的对象,列表是否有更多对象并不重要
hasItem
Checks just for one object it doesn't matter if the list has more
hasItem
只检查一个对象,列表是否有更多对象并不重要
All of them can receive a list of objects and use equals
method for comparation or can be mixed with other matchers like @borjab mentioned:
它们都可以接收对象列表并使用equals
方法进行比较,或者可以与提到的@borjab 等其他匹配器混合使用:
assertThat(myList , contains(allOf(hasProperty("id", is(7L)),
hasProperty("name", is("testName1")),
hasProperty("description", is("testDesc1"))),
allOf(hasProperty("id", is(11L)),
hasProperty("name", is("testName2")),
hasProperty("description", is("testDesc2")))));
http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#contains(E...)http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#containsInAnyOrder(java.util.Collection)http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#hasItems(T...)
http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#contains(E...) http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html #containsInAnyOrder(java.util.Collection) http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#hasItems(T...)
回答by Jim Jarrett
Make sure that the Object
s in your list have equals()
defined on them. Then
确保Object
列表中的s 已equals()
在它们上定义。然后
assertThat(generatedList,is(equalTo(expectedList)));
works.
作品。