创建一些Object类型的Mockito数组

iPh*_*Dev 5 java junit junit4 mockito

我需要提供一些这种类型的模拟对象数组"TypeA []".

我试图这样做但得到classcastexception:

List mockList = Mockito.anyListOf(TypeA.class);

when(someService.create(Mockito.any(TypeB.class), (TypeA[])mockList.toArray())).thenReturn(1);
Run Code Online (Sandbox Code Playgroud)

Don*_*ing 6

错误消息告诉您清楚:

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))
Run Code Online (Sandbox Code Playgroud)

Mockito.anyListOf返回的对象的方法调用只能在存根或验证中.

你可以简单地做到模拟数组:

when(mockTest.create(any(TypeB.class), any(TypeA[].class))).thenReturn(1);
Run Code Online (Sandbox Code Playgroud)