Mockito.any()匹配泛型类型T的任何实例

时间:2015-09-22 10:03:22

标签: java generics mockito

我试图模拟一个void方法,每次都抛出异常。该方法以List<SomeClass>为参数。如何将其传递给Mockito.any()

Mockito.doThrow(new Exception())
       .when(myClassInstanceSpy)
       .myVoidMethod(Mockito.any(List<SomeClass>.class)); // This fails!

这是我的班级定义:

class MyClass {
    ... 
    public void myVoidMethod(List<SomeClass> scList) {
        // ...
    }
}

1 个答案:

答案 0 :(得分:4)

尝试:

Mockito.doThrow(new Exception())
   .when(myClassInstanceSpy)
   .myVoidMethod(Mockito.anyListOf(SomeClass.class));