有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java PowerMockito 1.6.0验证Mockito扩展API中的私有最终重载方法

几天前发布了新版本的PowerMockito,支持验证私有/受保护的方法调用。虽然我使它在简单的情况下工作,但我缺少一些更“复杂”的功能。鉴于以下类别:

public class A {

    protected void myMethod(Exception... ex) {
        System.out.println("Method A");
    }

    protected void another() {
        System.out.println("Method A 1");
    }
}

:

public class B extends A {

    @Override
    protected void myMethod(Exception... ex) {
        System.out.println("Method B");
    }

    @Override
    protected void another() {
        System.out.println("Method B 1");
    }
}

:

public class C extends B {

    @Override
    protected void myMethod(Exception... ex) {
        System.out.println("Method C");
    }

    public void testMe() {
        myMethod(new NullPointerException("XXX"));
    }

    @Override
    protected void another() {
        System.out.println("Method C 1");
    }

    public void testMeAnother() {
        another();
    }
}

以及以下测试用例:

@PrepareForTest({ A.class, B.class, C.class })
public class MethodTest {

    @Test
    public void test() throws Exception {
        C classUnderTest = PowerMockito.mock(C.class);

        PowerMockito.doCallRealMethod().when(classUnderTest, "testMeAnother");
        PowerMockito.doCallRealMethod().when(classUnderTest, "testMe");
        PowerMockito.doCallRealMethod().when(classUnderTest, "myMethod");
        PowerMockito.doCallRealMethod().when(classUnderTest, "another");

        classUnderTest.testMeAnother();
        classUnderTest.testMe();

        //this works as expected
        PowerMockito.verifyPrivate(classUnderTest, times(1))
            .invoke(PowerMockito.method(C.class, "another"))
            .withNoArguments();

        //this raises an TooManymethodsFoundException:
        // Matching:
        // void myMethod(...)
        // void myMethod(...)
        // void myMethod(...)
        // three times!
        PowerMockito
            .verifyPrivate(classUnderTest, times(1))
            .invoke(PowerMockito.method(C.class, "myMethod",
            Exception[].class))
            .withArguments(any(Exception[].class));
    }
}

//是的,我直接调用mock上的方法,不管这个代码段是什么

给定测试的完整堆栈策略可以找到there

提前谢谢


共 (0) 个答案