如何JUnit测试两个List< E>包含相同顺序的相同元素? [英] How to JUnit test that two List<E> contain the same elements in the same order?

查看:713
本文介绍了如何JUnit测试两个List< E>包含相同顺序的相同元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的 JUnit 测试MyObject类.

I am writing a simple JUnit test for the MyObject class.

可以从采用

A MyObject can be created from a static factory method that takes a varargs of String.

MyObject.ofComponents("Uno", "Dos", "Tres");

MyObject存在期间的任何时间,客户端可以以

At any time during the existence of MyObject, clients can inspect the parameters it was created by in the form of a List<E>, through the .getComponents() method.

myObject.ofComponents(); // -> List<String>: { "Uno", "Dos", "Tres" }

换句话说,MyObject既记住并公开了使之成为现实的参数列表.有关此合同的更多详细信息:

In other words, a MyObject both remembers and exposes the list of parameters that brought it into existence. More details about this contract:

  • The order of getComponents will be the same as the one chosen for object creation
  • Duplicate subsequent String components are allowed and retained in order
  • Behaviour on null is undefined (other code guarantees no null gets to the factory)
  • There are no ways to alter the list of components after object instantiation

我正在编写一个简单的测试,该测试从

I am writing a simple test that creates a MyObject from a list of String and checks that it can return the same list via .getComponents(). I do this immediately but this is supposed to happen at a distance in a realistic code path.

这是我的尝试:

List<String> argumentComponents = Lists.newArrayList("One", "Two", "Three");
List<String> returnedComponents =
    MyObject.ofComponents(
        argumentComponents.toArray(new String[argumentComponents.size()]))
        .getComponents();
assertTrue(Iterables.elementsEqual(argumentComponents, returnedComponents));


问题

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