ssacher-tgm / mockito

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

Mockito gets mixed up stubbing an interface then a final method #198

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Stub an interface method using when
IFace myIface = mock(Iface.class);
when(myIface.method()).thenReturn(someResult);

2. Stub a final method on a concrete class
Class myClass = mock(Class.class);
when(myClass.finalMethod()).thenReturn(someResult);

What is the expected output? What do you see instead?
Expected: an error because this stubs a final method
Actual: there is no error, instead this appears to work but it has a subtle and 
strange problem - myIface.method() returns the myClass.finalMethod() result and 
myClass.finalMethod() returns null

What version of the product are you using? 
Mockito 1.8.5
Java 1.6

Original issue reported on code.google.com by andy.kri...@gmail.com on 14 Jun 2010 at 9:13

GoogleCodeExporter commented 8 years ago
Are you saying that this works ok, without throwing any errors:

when(myClass.finalMethod()).thenReturn(someResult);

?

Original comment by szcze...@gmail.com on 15 Jun 2010 at 3:23

GoogleCodeExporter commented 8 years ago
It doesn't throw an error but it also doesn't work ok. In this case, the final 
method stub returns null and the mock for the iface returns the result stubbed 
for the final method.

Original comment by andy.kri...@gmail.com on 15 Jun 2010 at 4:36

GoogleCodeExporter commented 8 years ago
Can you submit a test case that shows this problem?

I cannot reproduce it. 

//this fails gracefully for me all the time:
when(myClass.finalMethod()).thenReturn(someResult);

Original comment by szcze...@gmail.com on 21 Jun 2010 at 8:28

GoogleCodeExporter commented 8 years ago
Ok, I think I know what the problem is. Here's the limitation of the API:

myClass.nonFinalMethod();
when(myClass.finalMethod()).thenReturn(someResult);

Since mockito does not even know that you called a final method, mockito will 
think that you are actually stubbing nonFinalMethod(). This cannot be fixed 
because this is how java works (related to the evaluation order of arguments)

Original comment by szcze...@gmail.com on 10 Jul 2010 at 1:13

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 13 Aug 2010 at 8:50