Last active
March 3, 2017 11:08
-
-
Save dgageot/5445181 to your computer and use it in GitHub Desktop.
Mockito and java8
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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice java 8 sample, thanks