Skip to content

Instantly share code, notes, and snippets.

@SDiamante13
Last active March 13, 2020 01:19
Show Gist options
  • Select an option

  • Save SDiamante13/e7ce7d4c8f1d0970f43b9454cbeadc13 to your computer and use it in GitHub Desktop.

Select an option

Save SDiamante13/e7ce7d4c8f1d0970f43b9454cbeadc13 to your computer and use it in GitHub Desktop.
An example of using doAnswer when while testing an asynchronous method call.
@Test
public void getData_returnsData_whenAsyncThreadIsComplete() {
String expectedData = "Here's your data!";
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Runnable runnable = (Runnable) invocation.getArguments()[0];
runnable.run();
return null;
}
}).when(client).processData(any(Runnable.class));
String result = myService.getData().toString();
Assertions.assertThat(result).isEqualTo(expectedData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment