我有以下类,我试图阻止getString()实际被调用,但它似乎被调用,并且每次都抛出异常。我是否正确使用PowerMockito并进行间谍活动?
public abstract class AbstractClass {
protected String getString(String str){
//databse stuff causing exception
}
}
public class ImplClass extends AbstractClass{
//getInstance() and private contructor
public String method(){
String str = getString("str");
}
}
public class ImplClassTest{
public void testMethod(){
ImplClass impl = ImplClass.getInstance();
ImplClass spy = PowerMockito.spy(impl);
PowerMockito.doReturn("spy string").when(spy).method();
impl.method(); //this line is still getting exception
}
}
答案 0 :(得分:1)
而不是
impl.method(); //this line is still getting exception
你应该致电
spy.method();