【发布时间】:2018-03-17 19:53:31
【问题描述】:
我在我的一些静态字段上使用带有 Lombok 的 @Getter 表示法:
public class A {
@Getter protected static MyClass myClass;
}
在单元测试时,我必须为一段代码模拟这些静态字段的值:
MyClass.getMyClass();
为了模拟,我在做:
mock(MyClass.class);
when(MyClass.getMyClass()).thenReturn(...);
但是,这样的模拟给出了以下错误。
[testng] org.mockito.exceptions.misusing.MissingMethodInvocationException:
[testng] when() requires an argument which has to be 'a method call on a mock'.
[testng] For example:
[testng] when(mock.getArticles()).thenReturn(articles);
[testng]
[testng] Also, this error might show up because:
[testng] 1. you stub either of: final/private/equals()/hashCode() methods.
[testng] Those methods *cannot* be stubbed/verified.
[testng] Mocking methods declared on non-public parent classes is not supported.
[testng] 2. inside when() you don't call method on mock but on some other object.
我必须达到条件 2,但我不明白我为什么不是“在模拟上调用方法”。
有没有人成功模拟过 Lombok getter?
谢谢!
【问题讨论】:
-
getMyClass() 必须是静态的? mockito 支持静态方法模拟吗?
标签: java unit-testing mocking mockito lombok