我想模拟一个继承的受保护方法。由于该方法是从另一个包中的类继承而来,因此无法直接从Java代码中调用此方法。我找不到一种方法来指定在when(...)中进行存根的方法。
package a;
public class A() {
protected int m() {}
}
package b;
public class B extends a.A {
// this class currently does not override m method from a.A
public asd() {}
}
// test
package b;
class BTest {
@Test
public void testClass() {
B instance = PowerMockito.spy(new B());
PowerMockito.when(instance, <specify a method m>).thenReturn(123);
//PowerMockito.when(instance.m()).thenReturn(123); -- obviously does not work
}
}
我查看了PowerMockito.when
的重载方法,似乎它们都只能用于私有方法!
如何指定保护方法?