Enhancement: ArgumentCaptor.captureIf(ArgumentMatcher)
See original GitHub issueAssume we have the following test.
@Mock
private Consumer<Collection<?>> consumer;
@Test
public void test() throws Exception {
consumer.accept(new HashSet<>());
consumer.accept(new ArrayList<>());
ArgumentCaptor<ArrayList<?>> captor = ArgumentCaptor.forClass(ArrayList.class);
verify(consumer).accept(captor.capture());
}
The test fails with:
org.mockito.exceptions.verification.TooManyActualInvocations:
consumer.accept(<Capturing argument>);
Wanted 1 time:
-> at foo.TestClass.testName(TestClass.java:64)
But was 2 times. Undesired invocation:
-> at agh.TestClass.testName(TestClass.java:61)
at foo.TestClass.test(TestClass.java:64)
[..]
The test fails because the ArgumentCaptor stores every type, because the type information is erased a runtime. How can we capture the type that is specified in the captor, in this case ArrayList
? It is possible to implement an Answer
that stores the argument, and use
Matchers.isA(ArrayList.class)
but thats ugly->
doAnswer(captureArg1).when(consumer).accept(isA(ArrayList.class));
Maybe it is a good idea to create a pull req and extend the ArgumentCaptor API by adding a method like <T> T captureIf(ArgumentMatcher<? extends T>)
?
Then we could write:
verify(consumer).accept(captor.captureIf(isA(ArrayList.class)));
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:14 (7 by maintainers)
Top Results From Across the Web
Using Mockito ArgumentCaptor - Baeldung
ArgumentCaptor allows us to capture an argument passed to a method to inspect it. This is especially useful when we can't access the...
Read more >Mockito ArgumentCaptor, @Captor Annotation - DigitalOcean
Mockito ArgumentCaptor is used to capture arguments for mocked ... We can use @Captor annotation to create argument captor at field level.
Read more >Can I mix Argument Captor and a regular matcher?
In this case do I need to write a captor for each argument in spite of the fact I need to capture only...
Read more >Mockito 2 tutorial for beginners: Argument Matchers - YouTube
In this video tutorial of Mockito for beginners you will learn the how to implement argument matchers.The following topics are covered:1) ...
Read more >ArgumentCaptor (Mockito 2.28.2 API) - javadoc.io
custom argument matcher is not likely to be reused; you just need it to assert on argument values to complete verification. Custom argument...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@terebesirobert thank you for being open minded and considering my “hints”, good style of cooperation !
I have some questions about the user experience/expectation of the
captureIf(..)
API.Use Case 1:
What should be the content of
values
here?values
contains anArrayList
and aHashSet
instance.Use Case 2:
What should be the content of
values
here?values
contains 1 and -2 cause both arguments will be covered by capture() (equivalent tocaptureIf(any())
) and 1 will be covered bygt(0)
Use Case 3:
What should be the content of
values
here?values
contains 1 and null cause the arguments matchers a combined with or (isNull or isNotNull)