我需要提供一些这种类型的模拟对象数组"TypeA[]"。
我正在尝试这样做,但得到类转换异常:
List mockList = Mockito.anyListOf(TypeA.class);
when(someService.create(Mockito.any(TypeB.class), (TypeA[])mockList.toArray())).thenReturn(1);
错误消息清楚地告诉您:
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"))
对 Mockito.anyListOf 返回的对象的方法调用只能在存根或验证中。
你可以简单地这样做来模拟数组:
when(mockTest.create(any(TypeB.class), any(TypeA[].class))).thenReturn(1);