سؤال

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?

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top