Java PowerMockito spy未停止调用抽象方法
我有以下几个类,我试图阻止实际调用getString(),但它似乎每次都被调用并抛出异常。我是否正确使用PowerMockito和spyJava PowerMockito spy未停止调用抽象方法,java,powermock,Java,Powermock,我有以下几个类,我试图阻止实际调用getString(),但它似乎每次都被调用并抛出异常。我是否正确使用PowerMockito和spy public abstract class AbstractClass { protected String getString(String str){ //databse stuff causing exception } } public class ImplClass extends AbstractClass{
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
}
}
而不是
impl.method(); //this line is still getting exception
你应该打电话
spy.method();
我的英雄。所以当你创建一个间谍时,你应该仅仅使用间谍对象吗?换句话说,一旦你创建了一个间谍,就不应该使用原始的“真实”对象了?我对测试和模仿还不熟悉。您现在应该只使用间谍对象。祝你在测试中好运:)