0

I'm trying to mock Lucenes IndexReader.close() to do nothing.

I thought this should work...

    IndexReader reader = Mockito.mock(IndexReader.class);
    Mockito.stubVoid(reader).toReturn().on().close(); // old approach
    Mockito.doNothing().when(reader).close(); // new approach

but both result in the unit test calling the actual, real close method and ultimately causing a null pointer exception.

What have I missed?

TedTrippin
  • 3,525
  • 5
  • 28
  • 46

1 Answers1

3

As the javadoc indicates, close() is a final method. And Mockito can't mock final methods.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • :( O dear. Thanks JB. I don't suppose you have an idea how to get around without writing my own wrapper/interface? – TedTrippin Apr 25 '14 at 17:04
  • http://stackoverflow.com/questions/12139289/can-powermockito-mock-final-method-in-non-final-concrete-class could be the answer. – JB Nizet Apr 25 '14 at 17:08