在创建派生类的间谍的同时,我试图强制派生类中基类的构造函数的调用。
private class Base{
public Base(){
System.out.println("Base class mock invocation");
}
public Base(String args){
System.out.println("Base class spy invocation");
}
}
private class Derived extends Base{
public Derived(String args){
super("");
}
}
@Test
public void testBaseClass() throws Exception {
Base base = new Base();
PowerMockito.whenNew(Base.class).withAnyArguments().thenReturn(base);
Derived derived= Mockito.spy(new Derived("test"));
}
对派生类的间谍的调用忽略了基类构造函数的模拟。输出为:
基类模拟调用 基类间谍调用
是否可以强制用于间谍的基类的构造函数?