简体   繁体   English

如何模拟私有静态最终字段

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM