In mockito, is it possible to capture the previous value set to a field of an object passed to a mock object's method for example the method under test performs this
    public void methodUnderTest(){
    Person person = new Person();
    person.setAge(5);
    someObject.setPerson(person);
     ...
    person.setAge(6); 
    someObject.setPerson(person);
    }
What I wanted to know is if I mock someObject, will I be able to verify that someObject performed setPerson to a single object "person" twice but the object had different value for age when setPerson occurred? I tried using ArgumentCaptor but since I passed the same object, I was just able to get the last age.
    ArgumentCaptor<Integer> arg = ArgumentCaptor.forClass(Integer.class);
    verify(mockObject).setPerson(arg.capture());
    List<Integer> captureList = arg.getAllValues();
    captureList.get(0).getAge(); // returns 6
    captureList.get(1).getAge(); // returns 6
I also tried doing
    InOrder in = inOrder(mockObject);
    in.verify(mockObject).setPerson(arg.capture());
    assertEquals(5, arg.getValue().getAge()); //fails
                Unfortunately, you can't do that with an ArgumentCaptor.
The captor only stores a reference to the argument; in this case, it stores twice a reference to the same object, person.
You should try using an Answer, to perform the proper check every time the setPerson method is called.
This mockito issue is related to what you're trying to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With