我试图模拟我的私有(private)方法,但我得到了java.lang.IllegalArgumentException:对象不是声明类的实例
。下面是我的方法
private String decodeResponse(byte bresp[])
{
String spresp = null;
//
return spresp;
}
下面是我的测试类,
@PrepareForTest(MyClass.class)
@RunWith(PowerMockRunner.class)
public class MyClassTest{
@Test
public void test() throws Exception {
PowerMockito.spy(MyClass.class);
PowerMockito.doReturn("abcdefg").when(MyClass.class, "decodeResponse",Matchers.anyByte());
}
}
我在这个问题上被困了 3 个小时。任何帮助都将非常感激。
最佳答案
@PrepareForTest(MyClass.class)
@RunWith(PowerMockRunner.class)
public class MyClassTest{
@Test
public void test() throws Exception {
MyClass myClassSpy = PowerMockito.spy(MyClass.class);
PowerMockito.doReturn("abcdefg").when(myClassSpy, "decodeResponse",any(byte[].class));
}
}
关于java - 如何使用 Powermockito 模拟私有(private)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47337923/