我正在使用这样的powermock:
DetailServiceImpl impl=PowerMock.createPartialMockForAllMethodsExcept(
DetailServiceImpl.class, "getServiceDetail", JSONObject.class,String.class);
Whitebox.invokeMethod(impl,"getServiceDetail",response,dateFormat.format(date).toString());
但是,当运行此行时,将执行“getServiceDetail”并抛出空指针异常,因为在类DetailServiceImpl上模拟的方法使用由spring自动装配的类字段,但在此模拟上它为null。
当我对变量impl进行此赋值时,该方法已经运行,所以我不能这样做:
impl.setField(myFieldObjMock);
因为已经太晚了。
如何解决此问题,以便我可以在方法运行之前设置此字段,或者让powermock不将此字段保留为空?
____编辑:在代码示例_____
中添加了测试中的WhiteBox调用