开发者

createPartialMock with MocksControl

开发者 https://www.devze.com 2023-03-15 11:50 出处:网络
I have a StrictPartialMock (created with createStrictPartialMock(class, \"method1\")). and a normal mockedObject.

I have a StrictPartialMock (created with createStrictPartialMock(class, "method1")). and a normal mockedObject.

I want to test if method1() calls StrictPartialMock.method2(), mockedObject.method1(), StrictPartialMock.method3() in that order.

Now i read i can use private IMocksControl ctrl = createStrictControl(); to cre开发者_运维技巧ate a control that can check the order of method calls between mocks, but IMocksControl does not support createPartialMock().

Is there any way to combine these 2 techniques?


I've currently worked around this problem by extending the class i'm testing instead. I was already doing this because my class under test was abstract, but i guess this is possible as long as it's not final.

@test
public voide testCase() {
    IMocksControl ctrl = createStrictControl();
    OrderTester mockedOrderTester = ctrl.createMock(OrderTester.class);
    MyObject mockedMyObject = ctrl.createMock(MyObject.class);
    mockedOrderTester.method2()
    expectLastCall();
    mockedMyObject .method1()
    expectLastCall();
    mockedOrderTester.method3()
    expectLastCall();
    ctrl.replay();
    ClassUnderTestExt testClass = new ClassUnderTestExt();
    testClass.ot = mockedOrderTester;
    testClass.mo = mockedMyObject;
    testClass.method1();
    ctrl.verify();
}


class ClassUnderTestExt extends ClassUnderTest<String> {
    public OrderTester ot;
    public MyObject mo;

    @Override
    public void writeOutput(String data) {
    }

    @Override
    public void method2() {
        super.method2();
        ot.method2();
    }

    @Override
    public void method3() {
        super.method3();
        ot.method3();
    }
}

class OrderTester {
    public void method2() {
    }

    public void method3() {
    }
}

Notice you can leave out super.methodX(); to simulate true partialmocking.

But this solution is far from perfect. It would be way less work if this could have been done with partial mocking and a IMocksControl

0

精彩评论

暂无评论...
验证码 换一张
取 消