mockito InvalidUseOfMatchersException in PensieveSeekerTest

634 views
Skip to first unread message

krsk...@gmail.com

unread,
May 19, 2021, 6:23:01 PM5/19/21
to okapi-devel
I'm back to issue #837 (upgrading Lucene version).
There are two mockito based test cases in PensieveSeekerTest are throwing 
org.mockito.exceptions.misusing.UnfinishedStubbingException
instead of the expected OkapiIOException.

One of them looks like this:

@Test(expected = OkapiIOException.class)
public void iteratorNextIOException() throws Exception {
PensieveWriter writer = getWriter();
populateIndex(writer, 1, "patents are evil", "unittest");
writer.close();

Iterator<TranslationUnit> iterator = seeker.iterator();

IndexReader mockIndexReader = mock(IndexReader.class);
doThrow(new IOException("some exception")).when(mockIndexReader).document(anyInt());
Helper.setPrivateMember(iterator, "ir", mockIndexReader);

iterator.next();
}


The error message is:

java.lang.Exception: Unexpected exception, expected<net.sf.okapi.common.exceptions.OkapiIOException> but was<org.mockito.exceptions.misusing.InvalidUseOfMatchersException>
Caused by: org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at net.sf.okapi.tm.pensieve.seeker.PensieveSeekerTest.iteratorNextIOException(PensieveSeekerTest.java:185)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

at org.apache.lucene.index.IndexReader.document(IndexReader.java:352)
at net.sf.okapi.tm.pensieve.seeker.PensieveSeekerTest.iteratorNextIOException(PensieveSeekerTest.java:185)


It seems that this isn't working with the new Lucene version because the document(int)
method is now final while it wasn't in older Lucene versions, and Mockito does not
support stubbing final methods. 

Any suggestion how I port the test cases like this?

Kuro

krsk...@gmail.com

unread,
May 19, 2021, 6:39:32 PM5/19/21
to okapi-devel
NVM, this little magic worked: https://www.baeldung.com/mockito-final

jim

unread,
May 20, 2021, 2:31:22 PM5/20/21
to okapi...@googlegroups.com, krsk...@gmail.com
Thanks Kuro - this does bring up a general issue I've been wondering about. We have dependencies for powermock and mockito.  Do we need both? Is powermock a superset of mockito? If so should we standardize on powermock?

I want to encourage testing - but would also like to have a standard set of testing tools we can agree on.

thanks,

Jim
--
You received this message because you are subscribed to the Google Groups "okapi-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to okapi-devel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/okapi-devel/f42f60e3-f07e-4891-b45a-abc2333392fdn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages