Skip to content

Instantly share code, notes, and snippets.

@trevorrjohn
Created May 4, 2015 15:37
Show Gist options
  • Save trevorrjohn/b261b774560074b15e0d to your computer and use it in GitHub Desktop.
Save trevorrjohn/b261b774560074b15e0d to your computer and use it in GitHub Desktop.
builder pattern mock
/*
* 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