You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have some code that performs some work in sequence, performs an operation in a separate thread, and then performs some more work in sequence once the parallel work is complete. I would like to be able to verify that the final bit of work happened after the threaded work completed.
This Stack Overflow post explains the problem further, though it has the additional requirement of not caring about the relative order of the parallel work which is outside the scope of this feature request.
importorg.junit.Test;
importorg.mockito.InOrder;
importstaticorg.mockito.AdditionalAnswers.answersWithDelay;
importstaticorg.mockito.Mockito.*;
publicclassInOrderExampleTest {
@TestpublicvoiddoOrderedAndThreadedWork() {
MockedClassmockedClass = mock(MockedClass.class);
doAnswer(answersWithDelay(200, invocation -> null)).when(mockedClass).threadedMethod();
newInOrderExample(mockedClass).doOrderedAndThreadedWork();
InOrderinOrder = inOrder(mockedClass);
inOrder.verify(mockedClass).initialMethod();
inOrder.verify(mockedClass).threadedMethod();
inOrder.verify(mockedClass).finalMethod(); // Want to pass only if this is called after threadedMethod completes
}
}
This example should fail if the .join() call were removed, since the 200 ms delay on completing CompletableFuture.runAsync(mockedClass::threadedMethod) should take long enough to run that mockedClass.finalMethod() will be called before it completes.
This request is for a first-class way of verifying that a method happened after another method completes, rather than merely after the method was called.
The text was updated successfully, but these errors were encountered:
I have some code that performs some work in sequence, performs an operation in a separate thread, and then performs some more work in sequence once the parallel work is complete. I would like to be able to verify that the final bit of work happened after the threaded work completed.
This Stack Overflow post explains the problem further, though it has the additional requirement of not caring about the relative order of the parallel work which is outside the scope of this feature request.
This example should fail if the
.join()
call were removed, since the 200 ms delay on completingCompletableFuture.runAsync(mockedClass::threadedMethod)
should take long enough to run thatmockedClass.finalMethod()
will be called before it completes.I have the following workaround for now:
This request is for a first-class way of verifying that a method happened after another method completes, rather than merely after the method was called.
The text was updated successfully, but these errors were encountered: