debugjavaCritical
Mockito test a void method throws an exception
Viewed 0 times
mockitovoidmethodexceptionthrowstest
Problem
I have a method with a
The method when(T) in the type Stubber is not applicable for the arguments (void)
Any ideas how I can get the method to throw a specified exception?
void return type. It can also throw a number of exceptions so I'd like to test those exceptions being thrown. All attempts have failed with the same reason:The method when(T) in the type Stubber is not applicable for the arguments (void)
Any ideas how I can get the method to throw a specified exception?
doThrow(new Exception()).when(mockedObject.methodReturningVoid(...));Solution
The parentheses are poorly placed.
You need to use:
and NOT use:
This is explained in the documentation
You need to use:
doThrow(new Exception()).when(mockedObject).methodReturningVoid(...);
^and NOT use:
doThrow(new Exception()).when(mockedObject.methodReturningVoid(...));
^This is explained in the documentation
Code Snippets
doThrow(new Exception()).when(mockedObject).methodReturningVoid(...);
^doThrow(new Exception()).when(mockedObject.methodReturningVoid(...));
^Context
Stack Overflow Q#15156857, score: 1186
Revisions (0)
No revisions yet.