[英]How do I unit test for a Collection parameter with an Collection return?
I have a method that takes in a Collection of type T as a parameter and returns a Collection of type Integer. 我有一个方法,将T类型的Collection作为参数,并返回Integer类型的Collection。 In this specific instance, I'm trying to return an ArrayList (am I wrong to do that? I figured since an ArrayList inherits from a Collection, that should be okay).
在这个特定的实例中,我试图返回一个ArrayList(我做错了吗?我想是因为ArrayList继承自Collection,所以应该可以)。
@Test public void test() {
Collection<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(8);
list.add(7);
list.add(3);
list.add(4);
Comparison comp = new Comparison();
int low = 1;
int high = 5;
ArrayList<Integer> actual = SampleClass.<Integer>range(list, low, high, comp);
ArrayList<Integer> expected = new ArrayList<Integer>();
expected.add(2);
expected.add(3);
expected.add(4);
Assert.assertEquals(expected, actual);
}
What am I doing wrong here? 我在这里做错了什么?
EDIT: 编辑:
As requested, here is the discussed method: 根据要求,以下是讨论的方法:
public static <T> Collection<T> range(Collection<T> coll, T low, T high,
Comparator<T> comp) {
if (coll == null || comp == null) {
throw new IllegalArgumentException("No Collection or Comparator.");
}
if (coll.size() == 0) {
throw new NoSuchElementException("Collection is empty.");
}
ArrayList<T> al = new ArrayList<T>();
for (T t : coll) {
if (comp.compare(t, low) >= 0 && comp.compare(t, high) <= 0) {
al.add(t);
}
}
return al;
}
The comment from Andy Turner suggests copying the returned collection into an ArrayList
, which can then be compared to another ArrayList
using equals()
. Andy Turner的评论建议将返回的集合复制到
ArrayList
,然后可以使用equals()
将其与另一个ArrayList
进行比较。 This makes the code compile and presumably lets the test pass. 这使代码可以编译,并可能让测试通过。
The fact that the potential solutions include casting the result or copying it into a container of a different type raise some questions about what's actually going on here. 潜在的解决方案包括强制转换结果或将结果复制到其他类型的容器中,这一事实引发了一些有关此处实际发生情况的问题。
Copying the result Collection
into a List
makes some possibly unwarranted assumptions about the range()
method. 将结果
Collection
复制到List
会对range()
方法进行一些可能不必要的假设。 It's declared to return a Collection
. 声明返回一个
Collection
。 If your test is modified to copy the result to a List
and compares it to an expected result, also a List
, the test constrains the range()
implementation to produce its results in a particular order. 如果修改了测试以将结果复制到
List
并将其与预期结果(同样是List
进行比较,则测试将约束range()
实现以特定顺序产生其结果。 If order is significant, perhaps range()
should be modified to consume and return List
instead of Collection
. 如果顺序很重要,则可能应该修改
range()
以使用并返回List
而不是Collection
。
But what if order doesn't matter? 但是,如果顺序无关紧要怎么办? If the returned collection contained
[4, 3, 2]
instead of [2, 3, 4]
, would that be acceptable? 如果返回的集合包含
[4, 3, 2]
而不是[2, 3, 4]
,那可以接受吗? If so, then you'll need to compare the actual and expected results in an order-independent fashion. 如果是这样,那么您将需要以顺序无关的方式比较实际结果和预期结果。 For techniques to do that, see this question:
有关执行此操作的技术,请参见以下问题:
Is there a way to check if two Collections contain the same elements, independent of order? 有没有一种方法可以检查两个Collections是否包含相同的元素,而与顺序无关?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.