nus-cs2103-AY2223S2 / forum

12 stars 0 forks source link

AssertNotEquals for comparing reference (not value) #256

Closed adam07018 closed 1 year ago

adam07018 commented 1 year ago

Basically, I use a clone method to deep copy the Addressbook.

For example, AB2 = AB1.clone(); AB2 and AB1 has same userList but they got different reference.

Thus, If I write AssertNotEquals(AB1, AB2), it should pass test.

However, I found out that there is a equal method overloaded in Addressbook class which make my AssertNotEquals fail. Because equal method in Addressbook will return true if their userList are same.

public boolean equals(Object other) {
return other == this // short circuit if same object
                || (other instanceof AddressBook // instanceof handles nulls
                && clients.equals(((AddressBook) other).clients));
}

Is there anyway to bypass the overload equal and let test method (use the java object equal) directly compare their reference?

daitenshionyan commented 1 year ago

assertFalse(AB1 == AB2) should compare the references.

However I think a combination of both would be better. One to compare if they are different references and the other to check if they have the same state.

assertFalse(AB1 == AB2)
assertEquals(AB1, AB2)
adam07018 commented 1 year ago

Answer is found