22

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

0

2 Answers 2

42

Solution:

Mockito.doReturn(value) .when(mockObject) .myMethod(Mockito.any(MyExecutionContext‌​.class)) 
Sign up to request clarification or add additional context in comments.

Comments

7

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. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.