I was writing a Junit to test a scenario where an object changes its variables and saves it into the database 2 times. The argumentCaptor is invoked in save operation. The getAllValues() is returning two records. But both values are referenced to the same last capture record.
ImplimentationClass.java
... myObj.setVariable(oneValue); saveMyObj(myObj); myObj.setVariable(otherValue); saveMyObj(myObj); ... saveMyObj(MyObject myObj){ repository.save(myObj); } ImplimentationClassTest.java
private ImplimentationClass underTest ; @Mock private Repository mockRepository; @Before public void setup(){ initMocks(this); underTest = new ImplimentationClassTest(); } @Test public void test(){ ArgumentCaptor<MyObject> captor = ArgumentCaptor.forClass(MyObject.class); MyObject obj = new MyObject(value); underTest.implementedMethod(obj); verify(mockRepository, times(2)).save(captor.capture()); assertEquals(oneValue, captor.getAllValues().get(0).getVariable()); //failing here -getting otherValue assertEquals(otherValue, captor.getAllValues().get(1).getVariable()); } Any idea how to capture same object multiple times?