【发布时间】:2012-04-14 23:20:28
【问题描述】:
是否可以使用 Mockito 和可选的 Powermock 来模拟超类 S,以便模拟对超类的任何调用 S(包括对 S() 构造函数的调用)?因此,使用下面的示例,如果我使用 Mockito 将 S 替换为 MockS,那么对 super() 的调用是否会使用 MockS 中的构造函数?
class S {
S() {
// Format user's hard drive, call 911, and initiate self-destruct
}
}
class T extends S {
T() {
super();
}
}
class Test {
@Mock private S mockS;
new T(); // T's call to super() should call the mock, not the destructive S.
}
我在S 中看到了有关模拟单个方法或仅模拟对super() 的调用的问题,并且读到这是不受支持的,但尚不清楚我是否可以模拟整个超类。
在我当前的测试中,当我尝试模拟 S 时,T 对 super() 的调用调用的是真正的实现,而不是模拟。
【问题讨论】:
-
你的 mock 如何调用 super()?你能提供一个代码示例吗?
-
@nansen 我不是从模拟中调用
super(),模拟是超类。请查看我的编辑。 -
我不太喜欢
S的那门课。 -
@DerMike 为什么你认为我想嘲笑它?我只能调用一次。 :)
标签: java inheritance mockito superclass powermock