Mockito Exception - when() requires an argument which has to be a method call on a mock

Solution 1:

You need to create a MOCK of pcUserService first, and then use that mock.

PcUserService mock = org.mockito.Mockito.mock(PcUserService.class);
when(mock.read("1")).thenReturn(pcUser);

Solution 2:

In case others hit this issue....

It could also be the case that the method you are trying to mock out,pcUserService.read, is declared as a final method. From what I've noticed this appears to cause issues with Mockito.

Solution 3:

If you use Kotlin, you should know that methods are final by default. So write open fun instead of fun. Thanks to @djkelly99 for a tip.

Solution 4:

Another solution to this issue might be that in case of a test class that is using PowerMockRunner, you might have to add the class that you are mocking to the list, in @PrepareForTest annotation.

For instance -

@PrepareForTest({ PcUserService.class })