Reginald
Reginald

Reputation: 818

Mockito Error Is Not Applicable for the Arguments (void)

Mockito throws error "The method when(T) in the type Stubber is not applicable for the arguments (void)" for a class I'm mocking, can't figure out why.

the code in question is:

Mockito.when(mockObject.myMethod(Mockito.any(MyExecutionContext.class))).thenReturn(value);

I'm aware similar questions have been asked but if somebody could explain a solution for this or point me in the right direction I would greatly appreciate it

Upvotes: 22

Views: 59572

Answers (2)

PSN
PSN

Reputation: 71

Its likely that the return type of the method you are mocking (in above example: mockObject.myMethod) is VOID.

If the return type of the method is 'void' then use this format instead:

doThrow(new PersistenceException("Exception occured")).when(mockObject).myMethod(any());

// Note that we can never return a value when return type is void.

Upvotes: 7

Reginald
Reginald

Reputation: 818

Solution:

Mockito.doReturn(value)
       .when(mockObject)
       .myMethod(Mockito.any(MyExecutionContext‌​.class))

Upvotes: 41

Related Questions