Java 创建某种对象类型的Mockito数组
我需要提供一些这种类型的模拟对象数组“TypeA[]” 我正在尝试这样做,但得到classcastexception:Java 创建某种对象类型的Mockito数组,java,junit,mockito,junit4,Java,Junit,Mockito,Junit4,我需要提供一些这种类型的模拟对象数组“TypeA[]” 我正在尝试这样做,但得到classcastexception: List mockList = Mockito.anyListOf(TypeA.class); when(someService.create(Mockito.any(TypeB.class), (TypeA[])mockList.toArray())).thenReturn(1); 错误消息清楚地告诉您: You cannot use argument matchers
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);