Java 检查 Hamcrest 中的列表是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3631110/
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
Checking that a List is not empty in Hamcrest
提问by Ian Dallas
I was wondering if anyone knew of a way to check if a List is empty using assertThat()
and Matchers
?
我想知道是否有人知道使用assertThat()
and检查 List 是否为空的方法Matchers
?
Best way I could see just use JUnit:
我能看到的最好方法是使用 JUnit:
assertFalse(list.isEmpty());
But I was hoping that there was some way to do this in Hamcrest.
但我希望在 Hamcrest 有某种方法可以做到这一点。
采纳答案by skaffman
Well there's always
那么总是有
assertThat(list.isEmpty(), is(false));
... but I'm guessing that's not quite what you meant :)
...但我猜这不是你的意思:)
Alternatively:
或者:
assertThat((Collection)list, is(not(empty())));
empty()
is a static in the Matchers
class. Note the need to cast the list
to Collection
, thanks to Hamcrest 1.2's wonky generics.
empty()
是Matchers
类中的静态。注意需要转换list
到Collection
,由于Hamcrest 1.2的靠不住的仿制药。
The following imports can be used with hamcrest 1.3
以下导入可以与 hamcrest 1.3 一起使用
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.*;
回答by rafalmag
This is fixed in Hamcrest 1.3. The below code compiles and does not generate any warnings:
这在 Hamcrest 1.3 中得到修复。下面的代码编译并且不会产生任何警告:
// given
List<String> list = new ArrayList<String>();
// then
assertThat(list, is(not(empty())));
But if you have to use older version - instead of bugged empty()
you could use:
但是如果你必须使用旧版本 - 而不是窃听empty()
你可以使用:
hasSize(greaterThan(0))
(import static org.hamcrest.number.OrderingComparison.greaterThan;
orimport static org.hamcrest.Matchers.greaterThan;
)
hasSize(greaterThan(0))
(import static org.hamcrest.number.OrderingComparison.greaterThan;
或import static org.hamcrest.Matchers.greaterThan;
)
Example:
例子:
// given
List<String> list = new ArrayList<String>();
// then
assertThat(list, hasSize(greaterThan(0)));
The most important thing about above solutions is that it does not generate any warnings. The second solution is even more useful if you would like to estimate minimum result size.
上述解决方案最重要的是它不会产生任何警告。如果您想估计最小结果大小,则第二种解决方案更加有用。
回答by kamczak
If you're after readable fail messages, you can do without hamcrest by using the usual assertEquals with an empty list:
如果您需要可读的失败消息,则可以通过使用带有空列表的通常 assertEquals 来避免 hamcrest:
assertEquals(new ArrayList<>(0), yourList);
E.g. if you run
例如,如果你跑
assertEquals(new ArrayList<>(0), Arrays.asList("foo", "bar");
you get
你得到
java.lang.AssertionError
Expected :[]
Actual :[foo, bar]
回答by kamczak
Create your own custom IsEmpty TypeSafeMatcher:
创建您自己的自定义 IsEmpty TypeSafeMatcher:
Even if the generics problems are fixed in 1.3
the great thing about this method is it works on any class that has an isEmpty()
method! Not just Collections
!
即使泛型问题在1.3
这个方法的伟大之处得到修复,它也适用于任何具有isEmpty()
方法的类!不只是Collections
!
For example it will work on String
as well!
例如,它也将起作用String
!
/* Matches any class that has an <code>isEmpty()</code> method
* that returns a <code>boolean</code> */
public class IsEmpty<T> extends TypeSafeMatcher<T>
{
@Factory
public static <T> Matcher<T> empty()
{
return new IsEmpty<T>();
}
@Override
protected boolean matchesSafely(@Nonnull final T item)
{
try { return (boolean) item.getClass().getMethod("isEmpty", (Class<?>[]) null).invoke(item); }
catch (final NoSuchMethodException e) { return false; }
catch (final InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); }
}
@Override
public void describeTo(@Nonnull final Description description) { description.appendText("is empty"); }
}
回答by Richard
This works:
这有效:
assertThat(list,IsEmptyCollection.empty())