java JUnit 3 - 数组包含给定元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6470038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit 3 - Array contains a given element
提问by Tiago Veloso
I am trying to assert that a given array contains at least one instance of a given element. Is there an assert method that already does this? If so which one?
我试图断言给定数组至少包含给定元素的一个实例。是否有一个断言方法已经做到了这一点?如果有,是哪一个?
I am using Java6 and JUnit3.
我正在使用 Java6 和 JUnit3。
采纳答案by highlycaffeinated
Not a built-in assert, no. You'd need to use assertTrue()
and check the array yourself using something like Arrays.binarySearch()
, ArrayUtils.contains()
, or your own method.
不是内置断言,不是。你需要使用assertTrue()
,并检查使用类似自己阵列Arrays.binarySearch()
,ArrayUtils.contains()
或你自己的方法。
回答by Kevin Bowersox
You can cast the array to a list:
您可以将数组转换为列表:
assertTrue(Arrays.asList(yourArray).contains(yourElement));
回答by bheussler
assertThat(Arrays.asList(yourArray), hasItem(yourElement));
This will give you fine-grained information in the event of a test failure. It will print out your element and the collection it's looking in.
如果测试失败,这将为您提供细粒度的信息。它会打印出你的元素和它正在查找的集合。
回答by yoAlex5
You can try containsInAnyOrder
你可以试试 containsInAnyOrder
assertThat(actual, containsInAnyOrder(expected));