Skip to content

Commit

Permalink
Fixes mockito#2389 : Get existing stubbings with thread-safe method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikaël Francoeur committed Oct 12, 2021
1 parent e8f26b3 commit 8ef1562
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Expand Up @@ -20,7 +20,6 @@
import org.mockito.invocation.InvocationOnMock;
import org.mockito.mock.MockCreationSettings;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.Stubbing;

/**
* Returning deep stub implementation.
Expand Down Expand Up @@ -80,12 +79,10 @@ private Object deepStub(
throws Throwable {
InvocationContainerImpl container = MockUtil.getInvocationContainer(invocation.getMock());

// matches invocation for verification
// TODO why don't we do container.findAnswer here?
for (Stubbing stubbing : container.getStubbingsDescending()) {
if (container.getInvocationForStubbing().matches(stubbing.getInvocation())) {
return stubbing.answer(invocation);
}
StubbedInvocationMatcher existingStubbing =
container.findAnswerFor(container.getInvocationForStubbing().getInvocation());
if (existingStubbing != null) {
return existingStubbing.answer(invocation);
}

// record deep stub answer
Expand Down
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2021 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.defaultanswers;

import static org.mockito.Mockito.mock;

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

import org.junit.Test;
import org.mockito.Answers;

public class ReturnsDeepStubsConcurrentTest {

@Test
public void
given_mock_with_returns_deep_stubs__when_called_concurrently__then_does_not_throw_concurrent_modification_exception() {
for (int i = 0; i < 1000; i++) {
Service mock = mock(Service.class, Answers.RETURNS_DEEP_STUBS);
IntStream.range(1, 100).parallel().forEach(index -> mock.doSomething());
}
}

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

0 comments on commit 8ef1562

Please sign in to comment.