Last active
August 29, 2015 14:10
-
-
Save diversit/ef6a1025ac9d1dc2a113 to your computer and use it in GitHub Desktop.
Mockito: return default Optional.empty for unstubbed methods
This file contains 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
public static final Answer DEFAULT_OPTIONAL_EMPTY_ANSWER = invocation -> | |
invocation.getMethod().getReturnType() == Optional.class ? Optional.empty() : null; | |
Mockito.mock(YourClassWithOptionalReturnValues.class, DEFAULT_OPTIONAL_EMPTY_ANSWER); | |
// unfortunately this does not work since Mockito's Answers enum is not extendable (yet, issue 345) | |
@Mock(answer = DEFAULT_OPTIONAL_EMPTY_ANSWER) | |
private YourClassWithOptionalReturnValues mockInstance; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment