Skip to content

Instantly share code, notes, and snippets.

@aashreys
Last active October 27, 2023 23:48
Show Gist options
  • Save aashreys/e828cfaa0ed2136dd1b33a1b1f96a986 to your computer and use it in GitHub Desktop.
Save aashreys/e828cfaa0ed2136dd1b33a1b1f96a986 to your computer and use it in GitHub Desktop.
Use Mockito to instantly execute handler runnables in your tests.
// Creating a handler which executes runnables immediately
Mockito.when(uiHandler.post(Mockito.any(Runnable.class))).thenAnswer(
new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Runnable msg = invocation.getArgument(0);
msg.run();
return null;
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment