Last active
March 13, 2020 01:19
-
-
Save SDiamante13/e7ce7d4c8f1d0970f43b9454cbeadc13 to your computer and use it in GitHub Desktop.
An example of using doAnswer when while testing an asynchronous method call.
This file contains hidden or 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
| @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