Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 309
Reginald Avatar asked Apr 09 '15 11:04

Reginald


People also ask

How to do Mockito for void methods?

Mockito provides following methods that can be used to mock void methods. doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. doThrow() : We can use doThrow() when we want to stub a void method that throws exception.

How to mock private void method using Mockito?

For Mockito, there is no direct support to mock private and static methods. In order to test private methods, you will need to refactor the code to change the access to protected (or package) and you will have to avoid static/final methods.


1 Answers

Solution:

Mockito.doReturn(value)
       .when(mockObject)
       .myMethod(Mockito.any(MyExecutionContext‌​.class))
like image 180
Reginald Avatar answered Oct 29 '22 16:10

Reginald