java 比较 JUnit 中的两个对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36614556/
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
Comparing two list of objects in JUnit
提问by SRIRAM RAMACHANDRAN
I am writing JUnit Test case for my java project and using Coverage Tool to check the lines of code are covered. The problem is: I've two list of objects. I need to compare the results of object to be equal using assertTrue or any other possible assert statements. I am getting error like Assertion Error by using below assert statements. Is there any solution to compare two lists easily?
我正在为我的 java 项目编写 JUnit 测试用例,并使用 Coverage Tool 检查代码行是否被覆盖。问题是:我有两个对象列表。我需要使用 assertTrue 或任何其他可能的断言语句来比较对象的结果是否相等。通过使用下面的断言语句,我收到了像断言错误这样的错误。是否有任何解决方案可以轻松比较两个列表?
//actual
List<ProjectData> actuals = ProjectManagerDao.getProjects("x", "y", "z");
// expected
List<ProjectData> expecteds = new ArrayList<>();
ProjectData p1 = new ProjectData();
p1.setId("a");
p1.setName("b");
expecteds.add(p1);
assertTrue(JsonProvider.getGson().toJson(actuals).equalsIgnoreCase(JsonProvider.getGson().toJson(expecteds)));
//or
assertTrue(actuals.equalIgnoreCase(expeteds);//Not working for list of objects but working for comparing two strings
This differs from Java Compare Two Listsin that I need to be able to assert equality in jUnit, not just compare the lists.
这与Java 比较两个列表的不同之处在于我需要能够在 jUnit 中断言相等,而不仅仅是比较列表。
回答by GhostCat
In short: there is exactly one assert that one needs when writing JUnit tests: assertThat
简而言之:在编写 JUnit 测试时,确实需要一个断言:assertThat
You just write code like
你只需编写代码
assertThat("optional message printed on fails", actualArrayListWhatever, is(expectedArrayListWhatever))
where is()is a hamcrest matcher.
其中is()是一个 hamcrest 匹配器。
This approach does work for all kinds of collections and stuff; and it does the natural "compare element by element" thing (and there are plenty of other hamcrest matcher that one can use for more specific testing; and it is also pretty easy to write your own matchers).
这种方法适用于各种集合和东西;并且它执行自然的“逐个元素比较”的事情(还有很多其他 hamcrest 匹配器可以用于更具体的测试;编写自己的匹配器也很容易)。
( seriously; I have not seen a single case where assertThat() would not work; or would result in less readable code )
(说真的;我还没有看到 assertThat() 不起作用的情况;或者会导致代码可读性降低)
回答by Zbynek Vyskovsky - kvr000
Use Assert.assertArrayEquals(Object[] expecteds, Object[] actuals)method to compare two array for content equality:
使用Assert.assertArrayEquals(Object[] expecteds, Object[] actuals)方法比较两个数组的内容相等性:
Assert.assertArrayEquals(expected.toArray(), actuals.toArray());
equals
method compares arrays for being the same reference so it's not suitable in test cases.
equals
方法比较数组是否是相同的引用,因此它不适合在测试用例中。
For your other question: Remember to use org.junit.Assert not junit.Assert which became obsolete.
对于您的另一个问题:请记住使用 org.junit.Assert 而不是已经过时的 junit.Assert。
回答by Praveen Kumar
You can use
您可以使用
Assert.assertEquals(java.lang.Object expected, java.lang.Object actual);
example:
例子:
ArrayList<String> list1 = new ArrayList<>();
list1.add("hello");
ArrayList<String> list2 = new ArrayList<>();
list2.add("hello");
Assert.assertEquals(list1, list2);
source: Junit API
来源:Junit API
or if you want to use the arrays comparison method you can as below
或者如果你想使用数组比较方法,你可以如下
ArrayList<String> list1;
ArrayList<String> list2;
Assert.assertArrayEquals(list1.toArray(), list2.toArray());
I would recommend you to use the assertEquals method.
我建议您使用 assertEquals 方法。
PS: (the user defined objects stored inside the list should have the equals and hashcode methods overridden)
PS:(存储在列表中的用户定义的对象应该覆盖equals和hashcode方法)