Java powermock模拟私有方法,但不执行私有方法

Java powermock模拟私有方法,但不执行私有方法,java,methods,private,powermock,Java,Methods,Private,Powermock,如何模拟由公共方法调用的私有方法,但我不希望私有方法的代码执行: 守则: @RunWith(PowerMockRunner.class) @PrepareForTest(B.class) public class A { public static void main(String[] args) throws Exception{ B b = PowerMockito.spy(new B()); PowerMockito.doReturn("test")

如何模拟由公共方法调用的私有方法,但我不希望私有方法的代码执行: 守则:

@RunWith(PowerMockRunner.class)
@PrepareForTest(B.class)
public class A {
    public static void main(String[] args) throws Exception{
        B b = PowerMockito.spy(new B());
        PowerMockito.doReturn("test").when(b,"test");

       // MY PROBLEM: 
       // there: console print "test method" 
       // i just want return "test"  and don't real execute test()
        String t = b.print(); 

        System.out.println(t);
}
}


}我已经解决了这个问题! 只是: 模拟(B级) doCallRealMethod().when(…).withArguments(…)

不要使用spy()


谢谢大家。

您不想在如下方法中执行某行代码:
System.out.println(“测试方法”)?是的,不执行B#test()
class B{
    public String print(){
        System.out.println("pp method");
        return test();
    }
    private String test(){
        System.out.println("test method");  
        return "t";
    }