【问题标题】:GWT test with PowerMock and PowerMockito使用 PowerMock 和 PowerMockito 进行 GWT 测试
【发布时间】:2011-04-07 09:08:56
【问题描述】:

我有一个构造函数:

public PodLinksActivity( PodLinksPlace place ){
   super( MFactory.getView(), place);
    // other methods
}

如何使用 PowerMock 或 PowerMockito (Mockito) 对 MFactory.getView() 静态方法进行存根,以免生成 GWTTestCase?

谢谢!

【问题讨论】:

    标签: testing junit mockito powermock


    【解决方案1】:
    // view you expect to pass as first super-arg
    View view = mock(View.class);
    
    // setup the MFactory class
    PowerMockito.mockStatic(MFactory.class);
    // mock the method you care about
    PowerMockito.when(MFactory.class, "getView").thenReturn(view);
    

    确保在测试类的顶部添加适当的 PowerMock 注释:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(MFactory.class) 
    

    【讨论】:

    • 同样,您也可以使用存根 API:stub(method(MFactory.class, "getView")).toReturn(view)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多