以下代码导致org.mockito.exceptions.misusing.MissingMethodInvocationException:
Level level = mock(Level.class);
IOException ioException = new IOException("test");
when(level.getSyslogEquivalent()).thenThrow(ioException);
答案 0 :(得分:4)
您是否检查了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,它提供最终的类/方法模拟。
希望有所帮助