gpt4 book ai didi

java - 模拟 org.apache.log4j.Level 类时出现 MissingMethodInvocationException

转载 作者:行者123 更新时间:2023-11-30 06:29:22 26 4
gpt4 key购买 nike

以下代码导致 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,它提供最终类/方法模拟。

希望对你有帮助

关于java - 模拟 org.apache.log4j.Level 类时出现 MissingMethodInvocationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11615754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com