Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify that a call happened after the completion of a concurrent call #1903

Open
mjustin opened this issue Apr 10, 2020 · 3 comments
Open

Verify that a call happened after the completion of a concurrent call #1903

mjustin opened this issue Apr 10, 2020 · 3 comments

Comments

@mjustin
Copy link

mjustin commented Apr 10, 2020

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.

public interface MockedClass {
    void initialMethod();
    void threadedMethod();
    void finalMethod();
}
public class InOrderExample {
    private MockedClass mockedClass;

    public InOrderExample(MockedClass mockedClass) {
        this.mockedClass = mockedClass;
    }

    public void doOrderedAndThreadedWork() {
        mockedClass.initialMethod();
        CompletableFuture.runAsync(mockedClass::threadedMethod).join();
        mockedClass.finalMethod();
    }
}
import org.junit.Test;
import org.mockito.InOrder;

import static org.mockito.AdditionalAnswers.answersWithDelay;
import static org.mockito.Mockito.*;

public class InOrderExampleTest {
    @Test
    public void doOrderedAndThreadedWork() {
        MockedClass mockedClass = mock(MockedClass.class);

        doAnswer(answersWithDelay(200, invocation -> null)).when(mockedClass).threadedMethod();

        new InOrderExample(mockedClass).doOrderedAndThreadedWork();

        InOrder inOrder = 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.

I have the following workaround for now:

@Test
public void doOrderedAndThreadedWork() {
    MockedClass mockedClass = mock(MockedClass.class);
    CheckComplete methodComplete = mock(CheckComplete.class);

    doAnswer(answersWithDelay(200, invocation -> {methodComplete.complete(); return null;}))
            .when(mockedClass).threadedMethod();

    new InOrderExample(mockedClass).doOrderedAndThreadedWork();

    InOrder inOrder = inOrder(mockedClass);

    inOrder.verify(mockedClass).initialMethod();
    inOrder.verify(methodComplete).complete();
    inOrder.verify(mockedClass).finalMethod();
}

private interface CheckComplete {
    void complete();
}

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.

@borisenkotim
Copy link

I'll look into this issue

@mockitoguy
Copy link
Member

I'll look into this issue

@borisenkotim, thank you!

@mjustin
Copy link
Author

mjustin commented Oct 9, 2020

FYI, I just fixed a method name inconsistency and clarified the expectation in my issue summary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants