ArgumentCaptor should be type aware
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 problem here is that ArgumentCaptor is not type aware. That should be fixed.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:9 (7 by maintainers)
Top Results From Across the Web
How to use ArgumentCaptor for stubbing? - Stack Overflow
Assuming the following method to test: public boolean doSomething(SomeClass arg);. Mockito documentation says that you should not use captor in this way:
Read more >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 >ArgumentCaptor in Mockito - Spring Framework Guru
ArgumentCaptor in Mockito allows you to capture arguments passed to methods of Mockito mocks for further assertions.
Read more >Mockito argument matchers should be used on all parameters
Unicode-aware versions of character classes should be preferred ... anyInt(), i2); // Noncompliant ArgumentCaptor<Integer> captor = ArgumentCaptor.
Read more >Mockito ArgumentCaptor, @Captor Annotation - DigitalOcean
Mockito ArgumentCaptor is used to capture arguments for mocked methods ... We can write our test case and use ArgumentCaptor as shown below....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
What’s the status on that?
Type-safety is still something I am investigating, targeting Mockito 3. It seems that fixing it without strong typing, e.g. dynamically checking the generics, is very hacky and error-prone. It would be nice to have a solution where the compiler can catch these issues and make sure the verification is working, rather than on run-time failing.