Created
April 13, 2017 21:46
-
-
Save xdom/4a49b378347af0d25ada8dc1aadab87e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import org.mockito.ArgumentCaptor; | |
import org.mockito.Mockito; | |
import java.util.List; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import static org.junit.Assert.assertEquals; | |
import static org.mockito.Mockito.after; | |
import static org.mockito.Mockito.verify; | |
public class MockitoMultithreadedTest { | |
@Test | |
public void shouldCapture10Arguments() throws Exception { | |
final MockObj mockObj = Mockito.mock(MockObj.class); | |
ExecutorService exec = Executors.newFixedThreadPool(1); | |
exec.submit(() -> { | |
for (int j = 0; j < 10; j++) { | |
mockObj.method(0); | |
} | |
}); | |
ArgumentCaptor<Integer> argumentCaptor = ArgumentCaptor.forClass(Integer.class); | |
verify(mockObj, after(1000).atLeastOnce()).method(argumentCaptor.capture()); | |
List<Integer> captured = argumentCaptor.getAllValues(); | |
assertEquals(10, captured.size()); | |
} | |
private static class MockObj { | |
void method(Integer arg) { | |
// void | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment