Created
April 27, 2015 15:37
-
-
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 :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well this approach poses an issue when the save() in super class (BaseService) is calling some static methods.