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?
You can use the Deencapsulation.setField
method from the JMockit mocking library; 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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.