断言Junit中的List [英] Assert about a List in Junit

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

问题描述

如何在 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屋!

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