Search code examples
javaspringjunitmockito

Is ArgumentCaptor changing the value of a generated field?


I have an object with a field id, that is a generated UUID:

id = UUID.randomUUID().toString()

I use this in a method where I save the object:

saveObject(SomeObject someObject) {
    someRepository.saveObject(someTestObject.id, //other fields)
}

But when I use ArgumentCaptor in my test:

verify(someRepository, times(1)).save(ntCaptor.capture());
aasertThat(ntCaptor.getValue(),
    allOf(
        hasProperty("id", is(someObject.getId),
        // other fields
    )

I get a different id that I save. Is this a bug or is there a different approach to capture generated values?


Solution

  • In this:

    saveObject(SomeObject someObject) {
        someRepository.saveObject(someTestObject.id, //other fields)
    }
    

    maybe because you are not using the someObject, you are using someTestObject.