mardi 28 avril 2015

Partial mocking class under test

I'm looking to mock a static method of a support class and in order to do that, I need to mock a method of the class under test using jMockit. IN the example below, I want mock the method canContinue in order to always get inside the if condition. I also wnt to mock the static method and verify everything that happens after that.

public class UnitToTest {

    public void execute() {

        Foo foo = //
        Bar bar = //

        if (canContinue(foo, bar)) {
            Support.runStaticMethod(f);
            // Do other stuff here that I would like to verify 
        }
    }

    public boolean canContinue(Foo f, Bar b) {
        //Logic which returns boolean
    }   
}

My test method looks something like this:

@Test
public void testExecuteMethod() {

    // I would expect any invocations of the canContinue method to 
    // always return true for the duration of the test
    new NonStrictExpectations(classToTest) {{
        invoke(classToTest, "canContinue" , new Foo(), new Bar());
        result = true;
    }};

    // I would assume that all invocations of the static method
    // runStaticMethod return true for the duration of the test
    new NonStrictExpectations(Support.class) {{
        Support.runStaticMethod(new Foo());
        result = true;
    }};

    new UnitToTest().execute();

    //Verify change in state after running execute() method
}

What am I doing wrong here? Changing the first expectation for the canContinue method to return false doesnt influence whether the execution of code goes inside the if condition.

Aucun commentaire:

Enregistrer un commentaire