question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ArgumentCaptor should be type aware

See original GitHub issue

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

This issue was extracted from #358 and relates to #439.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
adriangaleracommented, Aug 8, 2019

What’s the status on that?

0reactions
TimvdLippecommented, Mar 27, 2017

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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found