PowerMock 访问私有成员

PowerMock access private members

看完后: https://code.google.com/p/powermock/wiki/BypassEncapsulation 我意识到,我不明白。

见本例:

public class Bar{
   private Foo foo;

   public void initFoo(){
       foo = new Foo();
   }
}

如何使用 PowerMock 访问私有成员 foo(例如验证 foo 不为空)?

注:
我不想要的是用额外的 get 方法修改代码。

编辑:
我意识到我在解决方案的链接页面上错过了示例代码块。

解法:

 Whitebox.getInternalState(bar, "foo");

这应该像编写以下测试一样简单 class:

public class BarTest {
    @Test
    public void testFooIsInitializedProperly() throws Exception {
        // Arrange
        Bar bar = new Bar();

        // Act
        bar.initFoo();

        // Assert
        Foo foo = Whitebox.getInternalState(bar, "foo");
        assertThat(foo, is(notNull(Foo.class)));
    }
}

添加正确的(静态)导入作为练习留给 reader :)。