vendredi 9 septembre 2016

Mockito says "Wanted but not invoked" but debugging test jumps into that method

I have to deal with a legacy application that has no tests. So before I begin refactoring I want to make sure everything works as it is.

Now imagine the following situation:

public SomeObject doSomething(final OtherObject x, final String something) {
    if(x != null) {
        final String = someOtherMethod(x, something);
    }
}

protected String someOtherMethod(final OtherObject x, final String something) {
    ....
}

Now I want to make sure that protected method is called as well

So I did this

@InjectMocks // Yes there is more going on here
private MyTestObject myTestObject;

private MyTestObject spy;

private static final OtherObject VALID_OTHER_OBJECT = new OtherObject();

@Before
public void setup() {
    this.spy = Mockito.spy(myTestObject);
}

@Test
public void ifOtherObjectIsNotNullExpectSubMethodToBeCalled() {
    myTestObject.doSomething(VALID_OTHER_OBJECT, null);
    verify(spy).someOtherMethod(VALID_OTHER_OBJECT, null);
}

I get a failing test and "Wanted but not invoked..." for someOtherMethod(). I jumped right into debug mode and checked. That method is called!

What could be the cause of this? Am I misusing the spy here?

Aucun commentaire:

Enregistrer un commentaire