如何断言列表与testng相等? [英] How to assert that lists are equal with testng?

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

问题描述

我找到了一个针对junit的答案,但需要一个testng解决方案.有什么想法比编写自己的for循环有用吗?

I found an answer for junit, but need a solution for testng. Any ideas more usefull as writing an own for loop?

推荐答案

不需要用于 List 比较的单独方法.可以通过 org.testng.Assert#assertEquals(Object,Object)比较两个列表.

There's no need for a separate method for List comparison. Two lists can be compared by org.testng.Assert#assertEquals(Object, Object).

如果两个列表 a b 都不为空,则调用 Assert.assertEquals(a,b)表示随后将调用 a.equals(b).

If two lists a and b are non-null, the call Assert.assertEquals(a, b) means a.equals(b) will be called subsequently.

您需要的是 java.util.List#equals ,如javadoc中所述:

And java.util.List#equals is what you need, as described in javadoc:

将指定的对象与此列表进行比较以确保相等.退货当且仅当指定对象也是一个列表时,才返回true具有相同的大小,并且两个中的所有对应的元素对列表是相等的.(如果(e1 == null?e2 == null:e1.equals(e2)).换句话说,两个列表被定义为如果它们包含相同顺序的相同元素,则相等.这定义可确保equals方法可跨List接口的不同实现.

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

这篇关于如何断言列表与testng相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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