Skip to content

Instantly share code, notes, and snippets.

@MaximeFrancoeur
Created April 27, 2015 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaximeFrancoeur/b6d6aef5e983734c2e2e to your computer and use it in GitHub Desktop.
Save MaximeFrancoeur/b6d6aef5e983734c2e2e to your computer and use it in GitHub Desktop.
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();
}
@deepakgupta8777
Copy link

Well this approach poses an issue when the save() in super class (BaseService) is calling some static methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment