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

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


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 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


Caused by: java.util.ConcurrentModificationException
	at java.util.LinkedList$ListItr.checkForComodification(
	at java.util.LinkedList$
	at org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs.deepStub(
	at org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs.answer(
	at org.mockito.Answers.answer(
	at org.mockito.internal.handler.MockHandlerImpl.handle(
	at org.mockito.internal.handler.NullResultGuardian.handle(
	at org.mockito.internal.handler.InvocationNotifierHandler.handle(
	at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(
	at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(
	at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptAbstract(
	at ConcurrencyTest$Service$MockitoMock$351714249.doSomething(Unknown Source)
	at ConcurrencyTest.lambda$test$0(
	at java.util.Spliterator$OfInt.forEachRemaining(
	at java.util.concurrent.CountedCompleter.exec(
	at java.util.concurrent.ForkJoinTask.doExec(
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(
	at java.util.concurrent.ForkJoinPool.runWorker(

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 <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

1 participant