我正在尝试比较 2 个列表:
assertThat(actual.getList(), is(Matchers.containsInAnyOrder(expectedList)));
但是想法
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)
我应该怎么写呢?
原文由 xander27 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您想断言这两个列表是相同的,请不要使用 Hamcrest 使事情复杂化:
如果你真的打算执行顺序不敏感的比较,你可以调用
containsInAnyOrder
可变参数方法并直接提供值:(假设您的列表是
String
,而不是Agent
,对于这个例子。)如果你真的想用
List
的内容调用相同的方法:如果没有这个,您将使用单个参数调用该方法并创建一个
Matcher
期望匹配一个Iterable
其中 每个元素 都是一个List
.这不能用于匹配List
。也就是说,您不能将
List<Agent>
与Matcher<Iterable<List<Agent>>
匹配,这是您的代码正在尝试的。