dhamini-poornachandra / mockito

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

Unable to mock java.nio.charset.Charset #327

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
public class MyTest{
  @Mock private Charset mockCharset;

  @Before
  public void setUp(){
    initMocks( this );
  }

  ...
}

Test methods in this class give 
java.lang.NullPointerException
       at java.nio.charset.Charset.hashCode(Charset.java:855)
       at java.util.HashMap.put(HashMap.java:372)
       at java.util.HashSet.add(HashSet.java:200)
       at org.mockito.internal.configuration.InjectingAnnotationEngine.scanMocks(InjectingAnnotationEngine.java:
146)
       at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:
102)
       at org.mockito.internal.configuration.InjectingAnnotationEngine.processInjectMocks(InjectingAnnotationEngine.java:
61)
       at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:
55)
       at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:
108)
       at MyTest.setUp( MyTestClass.java:xxx)

In other words, java.nio.charset.Charset cannot be mocked.  This issue is new 
in Mockito 1.9.0; that is, this class CAN be mocked in Mockito 1.8.5.  

Original issue reported on code.google.com by dmwallace.nz on 10 Mar 2012 at 2:41

GoogleCodeExporter commented 8 years ago
For the reference I'm putting informations that have been exchanged behind the 
scene.

The issue lies in "initMocks" which is using hash based collections. And 
hashCode & equals are final in Charset's class, that explains why there is an 
NPE.

Workaround is to not use the @Mock annotation in this very specific case. But 
rather
public MyTest {
    private Charset mockedCharset = mock(Charset.class);
}

More generally the future reader shall pay attention to similar use cases for 
final hashCode & equals method.

Charset charset = mock(Charset.class); // ok

Sets.newHashSet(charset); // <!-- NPE raised on hashCode
Lists.newArrayList(Charset.defaultCharset()).contains(charset); // NPE raised 
on equals

@David, I can patch it this week-end. Solution is fairly simple with a mock 
hash safe collection

Original comment by brice.du...@gmail.com on 10 Mar 2012 at 1:10

GoogleCodeExporter commented 8 years ago
Couldn't resist to fix it : 
revision 2a373dc755b4

Original comment by brice.du...@gmail.com on 11 Mar 2012 at 1:28

GoogleCodeExporter commented 8 years ago
Cool :)

Original comment by szcze...@gmail.com on 11 Mar 2012 at 5:38

GoogleCodeExporter commented 8 years ago
Brice - My client has asked me to pass on his thanks.

Original comment by dmwallace.nz on 11 Mar 2012 at 8:17

GoogleCodeExporter commented 8 years ago
A bug is a bug, and I'm on fire these days ;)

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

GoogleCodeExporter commented 8 years ago
Plus I want this release out :P

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

GoogleCodeExporter commented 8 years ago
Issue 328 has been merged into this issue.

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

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 13 May 2012 at 3:37

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 3 Jun 2012 at 2:06

GoogleCodeExporter commented 8 years ago

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