首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Assert等于Junit中的2个列表

Assert等于Junit中的2个列表
EN

Stack Overflow用户
提问于 2010-07-13 19:56:40
回答 13查看 254.2K关注 0票数 187

如何在JUnit测试用例中的列表之间进行相等断言?列表的内容之间应该相等。

例如:

代码语言:javascript
复制
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
EN

回答 13

Stack Overflow用户

发布于 2012-05-19 06:42:28

为了junit4!这个问题应该为写一个新的答案。

我意识到这个答案是在这个问题几年后写的,可能这个功能当时还没有出现。但现在,很容易做到这一点:

代码语言:javascript
复制
@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的最新版本,只需添加以下导入:

代码语言:javascript
复制
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

票数 184
EN

Stack Overflow用户

发布于 2014-10-18 00:23:47

不要转换为字符串和比较。这对性能不好。在junit中,在Corematcher内部,有一个=> hasItems的匹配器

代码语言:javascript
复制
List<Integer> yourList = Arrays.asList(1,2,3,4)    
assertThat(yourList, CoreMatchers.hasItems(1,2,3,4,5));

据我所知,这是检查列表中元素的更好方法。

票数 30
EN

Stack Overflow用户

发布于 2018-08-13 04:23:11

其他答案中提出的来自JUnit4/JUnit5的assertEquals(Object, Object)或来自Hamcrest的assertThat(actual, is(expected));只有在所比较对象的类(并且深度)都被覆盖时才能工作。

这很重要,因为断言中的相等性测试依赖于equals(),而测试失败消息依赖于所比较对象的toString()

对于内置的类,如StringInteger等,用于...没有问题,因为它们覆盖了equals()toString()。因此,使用assertEquals(Object,Object)断言List<String>List<Integer>是完全有效的。

关于这个问题:您必须在类中覆盖equals(),因为它在对象相等方面是有意义的,而不仅仅是为了在使用JUnit的测试中使断言更容易。

要使断言更容易,您可以使用其他方法。

作为一个好的实践,我更喜欢断言/匹配器库。

这是一个AssertJ解决方案。

org.assertj.core.api.ListAssert.containsExactly()是您所需要的:它验证实际的组是否完全包含给定值,而不包含其他值,顺序与javadoc中所述相同。

假设有一个Foo类,您可以在其中添加元素以及在哪里可以获得元素。

断言两个列表具有相同内容的Foo单元测试可能如下所示:

代码语言:javascript
复制
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

@Test
void add() throws Exception { 
   Foo foo = new Foo();
   foo.add("One", "Two", "Three");
   Assertions.assertThat(foo.getElements())
             .containsExactly("One", "Two", "Three");
}

AssertJ的一个优点是,按预期声明List是不必要的:它使断言更简洁,代码更具可读性:

代码语言:javascript
复制
Assertions.assertThat(foo.getElements())
         .containsExactly("One", "Two", "Three");

但是断言/匹配器库是必须的,因为这些库真的会更进一步。

现在假设Foo不存储String的实例,而存储Bar的实例。

这是一个非常普遍的需求。使用AssertJ,断言仍然很容易编写。更好的做法是,即使元素的类不覆盖equals()/hashCode(),您也可以断言列表内容是相等的,而JUnit方式要求:

代码语言:javascript
复制
import org.assertj.core.api.Assertions;
import static org.assertj.core.groups.Tuple.tuple;
import org.junit.jupiter.api.Test;

@Test
void add() throws Exception {
    Foo foo = new Foo();
    foo.add(new Bar(1, "One"), new Bar(2, "Two"), new Bar(3, "Three"));
    Assertions.assertThat(foo.getElements())
              .extracting(Bar::getId, Bar::getName)
              .containsExactly(tuple(1, "One"),
                               tuple(2, "Two"),
                               tuple(3, "Three"));
}
票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3236880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档