[英]How to mock private static final field
I have class which I need test: 我有课我需要测试:
class MyClass {
private final static MyOtherClass myOtherClass = OtherClassFactory.get();
public void doSomething() {}
}
How to mock myOtherClass
in unit test? 如何在单元测试中模拟
myOtherClass
?
You can use the Deencapsulation.setField
method from the JMockit mocking library; 您可以使用JMockit
Deencapsulation.setField
库中的Deencapsulation.setField
方法; it supports setting of final
fields as well (as long as the field is not initialized to a compile-time constant, which is not the case here). 它也支持
final
字段的设置(只要字段没有初始化为编译时常量,这不是这里的情况)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.