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

Parallel use of mocks with deep stubbing may lead to ConcurrentModificationException #2389

Closed
antonantsyferov opened this issue Aug 6, 2021 · 0 comments · Fixed by #2444
Closed

Comments

@antonantsyferov
Copy link

@antonantsyferov antonantsyferov commented Aug 6, 2021

Java8 / JUnit5
Mockito version: 3.11.2

Test class:

import org.junit.jupiter.api.RepeatedTest;
import org.mockito.Answers;

import java.util.List;
import java.util.stream.IntStream;

import static org.mockito.Mockito.mock;

class ConcurrencyTest {

    @RepeatedTest(value = 1000)
    void test() {
        final Service mock = mock(Service.class, Answers.RETURNS_DEEP_STUBS);
        IntStream.range(1, 100).parallel().forEach(i -> mock.doSomething());
    }

    interface Service {
        List<String> doSomething();
    }
}

Error: 30/5000 iterations fail with java.util.ConcurrentModificationException

Stacktrace:

Caused by: java.util.ConcurrentModificationException
	at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:966)
	at java.util.LinkedList$ListItr.next(LinkedList.java:888)
	at org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs.deepStub(ReturnsDeepStubs.java:85)
	at org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs.answer(ReturnsDeepStubs.java:75)
	at org.mockito.Answers.answer(Answers.java:99)
	at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:110)
	at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
	at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:34)
	at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:82)
	at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:56)
	at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptAbstract(MockMethodInterceptor.java:161)
	at ConcurrencyTest$Service$MockitoMock$351714249.doSomething(Unknown Source)
	at ConcurrencyTest.lambda$test$0(ConcurrencyTest.java:14)
	at java.util.stream.ForEachOps$ForEachOp$OfInt.accept(ForEachOps.java:204)
	at java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:110)
	at java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:693)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
	at java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290)
	at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:175)

P.S. If the Answers.RETURNS_DEEP_STUBS is removed, no error occurred.

LeMikaelF pushed a commit to LeMikaelF/mockito that referenced this issue Oct 12, 2021
LeMikaelF pushed a commit to LeMikaelF/mockito that referenced this issue Oct 12, 2021
TimvdLippe pushed a commit that referenced this issue Oct 13, 2021
Co-authored-by: Mikaël Francoeur <mikael.francoeur@ticketmaster.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

1 participant