Created
May 4, 2015 15:37
-
-
Save trevorrjohn/b261b774560074b15e0d to your computer and use it in GitHub Desktop.
builder pattern mock
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
/* | |
* copied from: http://stackoverflow.com/questions/8501920/how-to-mock-a-builder-with-mockito | |
*/ | |
import static org.mockito.Mockito.RETURNS_DEFAULTS; | |
import org.mockito.invocation.InvocationOnMock; | |
import org.mockito.stubbing.Answer; | |
public class SelfReturningAnswer implements Answer<Object>{ | |
public Object answer(InvocationOnMock invocation) throws Throwable { | |
Object mock = invocation.getMock(); | |
if( invocation.getMethod().getReturnType().isInstance( mock )){ | |
return mock; | |
} | |
else{ | |
return RETURNS_DEFAULTS.answer(invocation); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment