【问题标题】:Create Mockito Array of some Object type创建一些对象类型的 Mockito 数组
【发布时间】:2015-01-28 03:32:39
【问题描述】:
我需要提供一些“TypeA[]”类型的模拟对象数组。
我正在尝试这样做,但得到了 classcastexception:
List mockList = Mockito.anyListOf(TypeA.class);
when(someService.create(Mockito.any(TypeB.class), (TypeA[])mockList.toArray())).thenReturn(1);
【问题讨论】:
标签:
java
junit
mockito
junit4
【解决方案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);