断言Junit中的List [英] Assert about a List in Junit
问题描述
如何在 JUnit 测试用例中对列表进行断言?不仅列表的大小,而且列表的内容。
How to make assertions about a list in a JUnit test case? Not only the size of the list but also the contents of the list.
推荐答案
我意识到这是几年前被问到的,可能这个功能还没有。但是现在,这很容易做到:
I realise this was asked a couple years ago, probably this feature wasn't around then. But now, it's easy to just do this:
@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,只需添加这些导入:
If you have a recent version of Junit installed with hamcrest, just add these imports:
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
< a href =http://junit.org/junit4/javadoc/latest/org/hamcrest/core/Is.html =noreferrer> http://junit.org/junit4/javadoc/latest/org/ hamcrest / core / Is.html
这篇关于断言Junit中的List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!