【发布时间】:2016-09-17 17:36:54
【问题描述】:
如何通过 PowerMock 访问以下私有静态字段 foo。例如,我只想验证 Foo 不为空,并且我无法通过添加 getter 来重构代码。
public class Bar{
private static Foo foo = new Foo();
}
我尝试使用它,但它不起作用:
Foo foo = Whitebox.getInternalState(bar, "foo");
【问题讨论】:
-
试试
Whitebox.getInternalState(Bar.class, "foo")? -
它有效。非常感谢
-
我注意到您尚未接受任何答案。请考虑在某个时候这样做。
标签: java junit mockito powermock