2

以下代码导致 org.mockito.exceptions.misusing.MissingMethodInvocationException:

Level level = mock(Level.class);
IOException ioException = new IOException("test");
when(level.getSyslogEquivalent()).thenThrow(ioException);
4

1 回答 1

5

你检查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。

希望有帮助

于 2012-07-23T16:25:01.160 回答