Mockito:使用泛型列出匹配器

2022-01-14 00:00:00 warnings mockito java generics matcher

Mockito 提供:

Mockito offers:

when(mock.process(Matchers.any(List.class)));

如果 process 采用 List<Bar> 代替,如何避免警告?

How to avoid warning if process takes a List<Bar> instead?

推荐答案

对于Java 8 及以上,很简单:

For Java 8 and above, it's easy:

when(mock.process(Matchers.anyList()));

对于 Java 7 及以下版本,编译器需要一些帮助.使用 anyListOf(Class clazz):

For Java 7 and below, the compiler needs a bit of help. Use anyListOf(Class<T> clazz):

when(mock.process(Matchers.anyListOf(Bar.class)));

相关文章