我希望 A 类中继承的 getter 返回 MyObject 的 Mock。如果不直接在 A 中存根 getMyObject() 方法(即通过使用一些 MockBase 类),我怎么能做到这一点?
public class Base {
@Autowired
private MyObject obj;
public MyObject getMyObject(){
return obj;
}
}
public class A extends Base {
@Autowired
private SomeObject xyz;
public void myMethod(){
MyObject obj = getMyObject();
//do something here
}
}
public void MockBase {
@Mock
protected MyObject obj;
public MyObject getMyObject(){
return obj;
}
}
public class ATest extends MockBase {
@InjectMocks
private A a;
@Mock
private SomeObject xyz;
@BeforeMethod
public void setUp(){
MockitoAnnotations.initMocks(this);
}
public void MyMethod_SomeCondition_ExpectedResult{
a.myMethod();
}
}