我有一个课程
class MyClass{
void setX(){
statement1;
statement2;
obj.setY();
}
void setY(){
statement3;
anotherObj.setZ();
}
}
我想使用mockito将行elseObj.setZ()存根。我该怎么做。
答案 0 :(得分:1)
它取决于anotherObj初始化的位置。如果它在MyClass的构造函数中初始化,那么很容易:
AnotherObj anotherObjMock = Mockito.mock(AnotherObj.class);
MyClass classToTest = new MyClass(anotherObjMock);
Mockito.when(anotherObjMock.setZ()).thenReturn(whatever);
如果它是setY()方法的本地,那么它可能与PowerMock有关,但老实说它可能更多地表明你的代码编写得不好用于测试,你应该考虑重构。