在Junit中断言列表 [英] Assert List in Junit

查看:172
本文介绍了在Junit中断言列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 JUnit 测试用例中声明列表?

How to assert list in JUnit test case? I mean 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

http://junit.org/junit4/javadoc/latest /org/hamcrest/core/Is.html

这篇关于在Junit中断言列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆