当模拟 org.apache.log4j.Level 类时出现 MissingMethodInvocationException 异常

3
以下代码会导致 org.mockito.exceptions.misusing.MissingMethodInvocationException 异常:
Level level = mock(Level.class);
IOException ioException = new IOException("test");
when(level.getSyslogEquivalent()).thenThrow(ioException);
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是final的(我记得扩展了这个类,所以显然不是final)要么getSyslogEquivalent是final的(最好的猜测)。因此,您应该修改您的设计或测试设计,或者尝试使用Powermock,它提供了终极类/方法模拟。希望可以帮到您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接