PowerMockito间谍不会阻止抽象方法被调用

时间:2014-11-17 16:22:46

标签: java powermock

我有以下类,我试图阻止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
    }
}

1 个答案:

答案 0 :(得分:1)

而不是

impl.method(); //this line is still getting exception

你应该致电

spy.method();