joepadde / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

MockitoException on a method that is not being stubbed #451

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I have a test that fails inconsistently. When it does fail, the exception 
indicates that I was trying to setup a Mockito.when on a method that returns 
void.

'setResourceLimit' is a *void method* and it *cannot* be stubbed with a *return 
value*!
Voids are usually stubbed with Throwables:
    doThrow(exception).when(mock).someVoidMethod();

However, I was not, and the entire testcase does not use the setResourceLimit 
method. As I said, this only fails occasionally. If I was really trying to 
'when' a void method it would fail every time.

What seems to be happening is the 'setResourceLimit' method is found my Mockito 
instead of the method that I have actually called in the Mockito.when -- 
'canAcceptMoreWork(...).
Mockito.when(mockMediator.canAcceptMoreWork(mockCommandUtilization)).thenReturn(
true);

This is with Mockito 1.9.5 from a maven 3 project

Original issue reported on code.google.com by jw.ma...@gmail.com on 12 Sep 2013 at 12:56

GoogleCodeExporter commented 8 years ago
One more thing. The testcase does not always fail on that same line.  A 
different test just failed setting up a Mockito.when on the 
'reserveWorkCapacity' method with the same error I showed above:

Mockito.when(mockMediator.reserveWorkCapacity(mockCommandUtilization)).thenRetur
n(true);

Original comment by jw.ma...@gmail.com on 12 Sep 2013 at 1:17

GoogleCodeExporter commented 8 years ago
it might also be important to know that the class being mocked extends straight 
from Object and does not implement any interfaces.

I've been using Mockito for MANY years. I've never seen this. When it does 
fail, it is during a "mvn clean package", and the test in question is run by 
the failsafe plugin during the verify phase as it is an "*IT".

Original comment by jw.ma...@gmail.com on 12 Sep 2013 at 1:20

GoogleCodeExporter commented 8 years ago
In an effort to stabilize this test I extracted an interface from the class 
being mocked. It still fails, with a little lower frequency.

Original comment by jw.ma...@gmail.com on 18 Oct 2013 at 12:51

GoogleCodeExporter commented 8 years ago
Hi I think you may encounter an issue in your code base, where the mock is 
redefined after being used. This may happen when the mock is used/reused in an 
asynchronous environment. I've seen such cases when used in TestNG on on some 
occasion when run in JUnit code used as integration tests.

This is probably not an issue with mockito, but rather a misuse of the mock. 
You should follow the usage of your mocks. Mocks behavior can only be stubbed 
correctly and safely if done synchronously and usually before the test begins.

I don't have access to your code, so for now that's all I can say.

Cheers,
Brice

Original comment by brice.du...@gmail.com on 4 Dec 2013 at 2:26

GoogleCodeExporter commented 8 years ago
Thanks for the feedback. I actually no longer have access to the codebase where 
the situation was occurring. Recreating would be difficult. I'll ping the guys 
currently working on that project to see if it's still a problem, if they want 
to pursue this further.

Original comment by jw.ma...@gmail.com on 4 Dec 2013 at 2:49

GoogleCodeExporter commented 8 years ago
OK, thanks

Original comment by brice.du...@gmail.com on 4 Dec 2013 at 2:56