【发布时间】:2025-12-30 23:25:06
【问题描述】:
我正在测试使用继承方法的遗留代码。我正在尝试模拟超级方法 验证是否调用了超级方法。
@RunWith(PowerMockRunner.class)
public class HumanTest {
@Test
public void test() throws NoSuchMethodException, SecurityException {
// 1. arrange
Human sut = PowerMockito.spy(new Human());
PowerMockito.doNothing().when((SuperHuman) sut).run(); // SuperHuman is the parent class
// 2. action
sut.run();
// 3. assert / verify
}
}
public class Human extends SuperHuman {
@Override
public void run() {
System.out.println("human run");
super.run();
}
}
public class SuperHuman {
public void run() {
System.out.println("superhuman run");
}
}
我期待“human run”会被打印出来。但实际结果没有打印出来。
【问题讨论】: