如何使用 Powermock 和 mockito 返回对象数组

ash*_*ish 5 mockito powermock

我有一个返回对象数组的方法。

public IConfigurationElement[] getConfigurationElementsFor(String extensionPointId);

我不确定如何使用 mockito 和 powermock 来模拟这个调用。

我试过了

mockConfigurationElements = (IConfigurationElement[]) Mockito.anyListOf( IConfigurationElement.class ).toArray();

但这以ClassCastException.

mac*_*ias 4

模拟(存根)调用Mockito是通过以下方式完成的(例如):

Mockito.when(mockObject.getConfigurationElementsFor(Mockito.anyString()).thenReturn(new IConfigurationElement[]{})
Run Code Online (Sandbox Code Playgroud)

或者

Mockito.doReturn(new IConfigurationElement[]{}).when(mockObject).getConfigurationElementsFor(Mockito.anyString());
Run Code Online (Sandbox Code Playgroud)

Mockito.anyListOf()是匹配器的使用。存根时会传递匹配器而不是实际参数,这意味着如果使用满足这些匹配器的参数调用该方法,则将应用该行为。