如何在 JUnit 测试用例中的列表之间进行相等断言?列表的内容之间应该相等。
例如:
List<String> numbers = Arrays.asList("one", "two", "three");
List<String> numbers2 = Arrays.asList("one", "two", "three");
List<String> numbers3 = Arrays.asList("one", "two", "four");
// numbers should be equal to numbers2
//numbers should not be equal to numbers3
原文由 Kamal 发布,翻译遵循 CC BY-SA 4.0 许可协议
对于 junit4!这个问题值得为 junit5 写一个新的答案。
我意识到这个答案是在问题出现几年后写的,可能当时还没有这个功能。但是现在,很容易做到这一点:
如果你安装了最新版本的 Junit 和 hamcrest,只需添加这些导入:
http://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThat(T, org.hamcrest.Matcher)
http://junit.org/junit4/javadoc/latest/org/hamcrest/CoreMatchers.html
http://junit.org/junit4/javadoc/latest/org/hamcrest/core/Is.html