以下代码导致 org.mockito.exceptions.misusing.MissingMethodInvocationException:
Level level = mock(Level.class);
IOException ioException = new IOException("test");
when(level.getSyslogEquivalent()).thenThrow(ioException);
你检查MissingMethodInvocationException
异常消息了吗?
它说 :
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
It is a limitation of the mock engine.
该类Level
是最终的(显然不是我记得扩展该类)或者getSyslogEquivalent
是最终的(最好的猜测)。
因此,您应该修改您的设计或测试设计,或者尝试使用提供最终类/方法模拟的 Powermock。
希望有帮助