如何在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
对于junit4!这个问题值得为junit5编写一个新的答案。
我意识到这个答案是在提出问题几年后写的,可能当时还没有这个功能。但是现在,很容易做到这一点:
@Test public void test_array_pass() { List<String> actual = Arrays.asList("fee", "fi", "foe"); List<String> expected = Arrays.asList("fee", "fi", "foe"); assertThat(actual, is(expected)); assertThat(actual, is(not(expected))); }
如果您安装了带有 hamcrest 的最新版本的 Junit,只需添加这些导入:
import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*;
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