Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return Array of Objects using Powermock and mockito

I have a method which return array of object.

public IConfigurationElement[] getConfigurationElementsFor(String extensionPointId);

I am not sure how can I mock this call using mockito and powermock.

I have tried

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

but this is ending in ClassCastException.

like image 632
Ashish Avatar asked Oct 01 '22 12:10

Ashish


1 Answers

Mocking (stubbing) calls with Mockito is done in a following way (for example):

Mockito.when(mockObject.getConfigurationElementsFor(Mockito.anyString()).thenReturn(new IConfigurationElement[]{})

or

Mockito.doReturn(new IConfigurationElement[]{}).when(mockObject).getConfigurationElementsFor(Mockito.anyString());

Mockito.anyListOf() is a use of a matcher. Matchers are passed instead of real arguments when stubbing meaning that the behavior is to be applied if the method is called with arguments satisfying those matchers.

like image 170
macias Avatar answered Oct 03 '22 02:10

macias