0 votes
1 view
in Java by (4.1k 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);

verify(mockBar).doSomething(firstFooCaptor.capture());

verify(mockBar).doSomething(secondFooCaptor.capture());

// 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 (46.1k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...