mercredi 18 novembre 2015

Testing Delegate Method with Mockito

In the class like the below the only tests required around doActionOne() and doActionTwo() are to ensure that they delegate to doAction() with the correct parameters.

public class ClassUnderTest {

    public void doActionOne(String a, int b) {
        doAction(a, b, true);
    }

    public void doActionTwo(String a, int b) {
        doAction(a, b, false);
    }

    public void doAction(String a, int b, boolean c) {
        //already tested
    }
}

Such a test would seem to require some kind of partial mock or spy however I am unable to get this correct.

The test should look something like the below although this approach doesn't work.

@Test
public void testDoActionOne(){
    ClassUnderTest cut = Mockito.mock(ClassUnderTest.class);

    //call the real method
    when(cut.doActionOne("A1", 1)).thenCallRealMethod();

    //test delegate called with correct params
    verify(cut, times(1)).doAction("A1", 1, true); //fails wanted but not invoked
}

Not sure if I need something like:

http://ift.tt/1MUu8BX

Aucun commentaire:

Enregistrer un commentaire