Skip to content

Instantly share code, notes, and snippets.

@dgageot
Last active March 3, 2017 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgageot/5445181 to your computer and use it in GitHub Desktop.
Save dgageot/5445181 to your computer and use it in GitHub Desktop.
Mockito and java8
import static org.mockito.Mockito.*;
import java.util.concurrent.*;
import org.junit.*;
import org.mockito.*;
import org.mockito.stubbing.*;
public class TestMockito {
@Test
public void should_use_java_8() {
Executor executor = mock(Executor.class);
doExecute((arg) -> ((Runnable) arg).run()).when(executor).execute(any(Runnable.class));
}
private static Stubber doExecute(InvocationWithOneArg invocationWithOneArg) {
return Mockito.doAnswer((invocation) -> {
invocationWithOneArg.invoke(invocation.getArguments()[0]);
return null;
});
}
private static Stubber doExecute(InvocationWithTwoArgs invocationWithTwoArgs) {
return Mockito.doAnswer((invocation) -> {
invocationWithTwoArgs.invoke(invocation.getArguments()[0], invocation.getArguments()[1]);
return null;
});
}
public static interface InvocationWithOneArg {
void invoke(Object arg1);
}
public static interface InvocationWithTwoArgs {
void invoke(Object arg1, Object arg2);
}
}
@enricjaen
Copy link

nice java 8 sample, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment