dhamini-poornachandra / mockito

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

Cannot mock an object that has a final throwing implementation of hashCode #328

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
class Orange {
    @Override public final int hashCode() {
      throw new RuntimeException("oops");
    }
  }

  @Mock private Orange orange;

What is the expected output? What do you see instead?
Orange's hashCode is called in initMocks and an exception is thrown

What version of the product are you using? On what operating system?
1.9.0 - This is a regression from 1.8.5

Please provide any additional information below.
I poked around at the code and it looks like this was inadvertently triggered 
by the reimplementation of InjectMocks.
Probably want to use a List in InjectingAnnotationEngin.scanMocks instead of a 
Set

Original issue reported on code.google.com by andrewea...@google.com on 15 Mar 2012 at 4:37

GoogleCodeExporter commented 8 years ago
Forgot to add - this issue is blocking adoption for us since it introduces a 
bunch of failing tests

Original comment by andrewea...@google.com on 15 Mar 2012 at 4:41

GoogleCodeExporter commented 8 years ago
Hi Andrew,

As you may know mockito cannot mock final classes nor final methods, this 
something the JVM itself is enforcing, and Mockito normally stubs these 
methods. Unfortunatley these methods being final we can't overide them and 
initMocks now uses hash based collections, which will of course raises 
exception in your case.

This regression was missed during the 1.9.0 RC period, but now the fix is 
already in the default branch and will be released in version 1.9.1.

Thanks for reporting :)

I'm interested, you are marking hashcode and most probably equal as final, 
could you elaborate why ?
Because in my opinion it feels weird to mark only these methods as final, I 
would rather make the whole class final.

Original comment by brice.du...@gmail.com on 15 Mar 2012 at 11:55

GoogleCodeExporter commented 8 years ago
Awesome - any timeline on when this will be released?

I'm not sure of the details - I was upgrading the version of Mockito
used internally by Google and ran into this issue. I was just trying
to make a simple repro here for the bug report

Original comment by andrewea...@google.com on 16 Mar 2012 at 1:33

GoogleCodeExporter commented 8 years ago
About the next version, we will include Android support. And with introduced 
some changes, I'd like to verify if the current API can work for us for other 
plugins.

Once globally OK, I think we can release. Of course the issue here is that 
development is made on our scarce free time, so I'm not yet sure of the time 
line.

Though it might be possible to release an 1.9.1-RC.

Original comment by brice.du...@gmail.com on 17 Mar 2012 at 4:20

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 3 Sep 2012 at 10:00

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 3 Sep 2012 at 10:11