如何使用Power Mockito模拟私有方法?

时间:2017-11-16 19:47:25

标签: java unit-testing junit powermockito

我正在尝试模仿我的私有方法,但我得到java.lang.IllegalArgumentException: object is not an instance of declaring class。我的方法是

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个小时我遇到了这个问题。任何帮助都会非常值得赞赏。

1 个答案:

答案 0 :(得分:0)

    @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));
    }   
    }
相关问题