Ho una struttura di oggetti abbastanza complessa (con un mucchio di campi primitivi e riferimenti a oggetti) e voglio testare tutti i campi tranne - alcuni - di loro. Come esempio;Hamcrest - Un modo elegante per testare oggetti complessi con samepropertyvaluesas
ComplexObject actual = generateMagically("someInput");
ComplexObject expected = ActualFunction.instance.workMagically(actual);
// we want to be sure that workMagically() would create a new ComplexObject
// with some fields are different than "actual" object.
// assertThat(actual, samePropertyValuesAs(expected)); would check all fields.
// what I want is actually; - notice that "fieldName1" and "fieldName2" are
// primitives belong to ComplexObject
assertThat(actual, samePropertyValuesExceptAs(expected, "fieldName1", "fieldName2"))
Dal momento che non voglio controllare tutti i campi manualmente, credo che ci deve essere un modo di scrivere che prova con eleganza. Qualche idea?
Cheers.
modo da avere due oggetti * * bag e desidera eseguire il confronto profondo? – Raffaele
Non è sicuro chiamarli oggetto bag, ha molti campi primitivi e altri riferimenti a oggetti. I campi che voglio saltare sono quelli primitivi che appartengono a ComplexObject. Possiamo dire che sarà un confronto profondo. – tugcem
Vuoi solo l'implementazione di Matcher per 'samePropertyValuesExceptAs' come risposta a questa domanda? In tal caso, è sufficiente creare una copia di 'org.hamcrest.beans.SamePropertyValuesAs' e aggiungervi un altro costruttore/metodo factory statico che rimuoverà le proprietà escluse dalla fase di test. –
SpaceTrucker