Skip to content

Instantly share code, notes, and snippets.

@tgoossens
Created March 30, 2013 16:15
Show Gist options
  • Save tgoossens/5277286 to your computer and use it in GitHub Desktop.
Save tgoossens/5277286 to your computer and use it in GitHub Desktop.
needs mockito library
public class StreamMock {
/**
* Will create a mock object. When one of the methods of this mock object is
* invoked, it will write information about this method-call to the given
* printstream.
*
* @param clazz
* @param out
* @return
*/
public static <T> T streammock(final Class<T> clazz, final PrintStream out) {
final T t = Mockito.mock(clazz, new Answer<T>() {
@Override
public T answer(InvocationOnMock invocation) throws Throwable {
final String methodname = invocation.getMethod().getName();
final String str = new StringBuilder()
.append("mock: ")
.append(invocation.getMethod().getDeclaringClass()
.getCanonicalName()).append(".")
.append(methodname)
.append(Arrays.asList(invocation.getArguments()))
.append(";").toString();
out.println(str);
return null;
}
});
return t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment