I have a very simple test case that is using Mockito and Spring Test framework. When I do
when(pcUserService.read(\"1\")).thenReturn(pcUser);
There's another possible reason for such error - sometimes IDE prefers to statically import Mockito.when() from another package:
import static io.codearte.catchexception.shade.mockito.Mockito.when;
vs
import static org.mockito.Mockito.when; //should normally use this one
The thing is 'when' from io.codearte package is compliant with org.mockito.Mockito.any() on compilation level, but fails during runtime with that exact same error message.
I had the same issue, the method that I was trying to mock it was a final method. I removed the modifier and it worked fine.
Basically You need to use the PowerMockito.mockStatic to enable static mocking for all static methods of a class. This means make it possible to stub them using the when-thenReturn syntax.
For example:
PowerMockito.mockStatic(TestClass.class);
when(TestClass.getString()).thenReturn("HelloWorld!");
Note: you have to add @PrepareForTest({ TestClass.class })
to your unit test class.