MaximeFrancoeur
4/27/2015 - 3:37 PM

Mockito How to mock only the call of a method of the superclass ? No, Mockito does not support this. If you really don't have a choice fo

Mockito How to mock only the call of a method of the superclass ?

No, Mockito does not support this.

If you really don't have a choice for refactoring you can mock/stub everything in the super method call :

class BaseService {
    public void save(){
        validate();
    }
}

public ChildService extends BaseService{
    public void save(){
        super.save()
        load();
    }
}

@Test
public void testSave() {
    ClildService spy = Mockito.spy(new ChildService());

    // Prevent/stub logic in super.save()
    Mockito.doNothing().when((BaseService)spy).validate();

    // When
    spy.save();

    // Then
    verify(spy).load();
}