我在测试类中使用Powermock模拟了类的静态方法。我面临的问题是,此静态方法不是在测试类中直接调用,而是在其他一些类中直接调用。这是骨架:
@Test public void myTest() {
PowerMockito.mockStatic(MyClassWithStaticMethod.class);
PowerMockito.when(MyClassWithStaticMethod.staticMethod()).thenReturn("...");
List<String> details = MyHelperClass.getDetails();
...
...
}
现在,MyHelperClass.getDetails调用了需要模拟的方法,因为该方法依赖于服务。
MyHelperClass.java
public static List<String> getDetails() {
...
...
MyObj obj = MyClassWithStaticMethod.staticMethod(); //This needs to return mocked value
...
...
}
有没有一种方法可以将模拟对象传递给助手类?PowerMockito.mockStatic返回void,因为PowerMockito.mock不模拟静态方法。因此,我无法弄清楚如何将模拟对象传递给助手类的getDetails()方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句