【发布时间】:2021-01-15 19:18:54
【问题描述】:
我有一个从 B 扩展而来的 A 类。B 有一个名为 doB() 的方法。
在我的测试中,我想在调用 doB() 时返回 A。
B类:
public String doB() {
return "B";
}
A类:
public class A extends B {
public String doA() {
String A = doB();
return A;
}
}
在我的单元测试中,我想做这样的事情:
@Mock
private A a;
@Test
public void test() {
when(a.doB()).thenReturn("PASS");
a.doB();
//check if it is "PASS"
}
但是,它在调用doB() 时不会拦截。这可能吗?如果没有,最好的测试方法是什么?
【问题讨论】: