Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (4k points)

I have a method that gets called twice, and I want to capture the argument of the second method call.

Here's what I've tried:

ArgumentCaptor<Foo> firstFooCaptor = ArgumentCaptor.forClass(Foo.class);

ArgumentCaptor<Foo> secondFooCaptor = ArgumentCaptor.forClass(Foo.class);



// then do some assertions on secondFooCaptor.getValue()

But I get a TooManyActualInvocations Exception, as Mockito thinks that doSomething should only be called once.

How can I verify the argument of the second call to doSomething?

1 Answer

0 votes
by (46k points)

I believe it should be

verify(mockBar, times(2)).doSomething(...)

Example from mockito javadoc:

ArgumentCaptor<Person> peopleCaptor = ArgumentCaptor.forClass(Person.class);

verify(mock, times(2)).doSomething(peopleCaptor.capture());

List<Person> capturedPeople = peopleCaptor.getAllValues();

assertEquals("John", capturedPeople.get(0).getName());

assertEquals("Jane", capturedPeople.get(1).getName());

Related questions

Browse Categories