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
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.
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.
Solution:
Mockito.doReturn(value)
.when(mockObject)
.myMethod(Mockito.any(MyExecutionContext.class))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With