I tried in the first time to Override my class(Money) to check two objects on a specific attribute. a second time I tried to check two same objects from the same class(Money) but it didn't work.
I tried Overriding Equals() and Hashcode()
On the first class(Money). On the top of file.
public static final Money HALF_DINAR = new Money(BigDecimal.valueOf(0.50));
public BigDecimal current_Money;
public static final Money ZERO =new Money(BigDecimal.ZERO);
public static Money mInsideMachine = new Money(BigDecimal.ZERO);
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Money)) {
return false;
}
Money money = (Money) o;
return new EqualsBuilder()
.append(this.current_Money,money.current_Money)
.isEquals();
}
@Override
/*public int hashCode() {
return Objects.hash(current_Money);
}*/
public int hashCode(){
return new HashCodeBuilder(17, 37)
.append(current_Money)
.toHashCode();
}
The command I using to check the Objects is
assertThat(snackMachine.moneyInside()).isEqualTo(Money.HALF_DINAR);
*Be noted: the object snackMachine is from class SnackMachine but the return is from Money's class. here is the code for moneyInside.
public Money moneyInside() {
return Money.mInsideMachine;
}
The Code of second file(non changable)(SnackMachine class) Unit test
@Test
void buying_a_snack_after_inserting_just_enough_money_then_the_money_inside_should_equals_to_money_inserted() {
snackMachine.insertMoney(Money.QUARTER_DINAR);
snackMachine.insertMoney(Money.QUARTER_DINAR);
snackMachine.buySnack(SnackType.CHEWING_GUM);
System.out.println("Moneyinside (Unit test)"+Money.mInsideMachine.current_Money.toString());
System.out.println("Money half dinar (Unit test)"+Money.HALF_DINAR.current_Money.toString());
//assertThat(snackMachine.moneyInTransaction()).isEqualTo(Money.ZERO);
assertThat(snackMachine.moneyInside()).isEqualTo(Money.HALF_DINAR);
}
I added some code to print the values and the output is:
Moneyinside (Unit test)0.50
Money half dinar (Unit test)0.5
org.opentest4j.AssertionFailedError:
Expecting:
<com.progressoft.induction.Money@885>
to be equal to:
<com.progressoft.induction.Money@311>
but was not.
Expected :com.progressoft.induction.Money@311
Actual :com.progressoft.induction.Money@885
The issue here is that 0.5 is not equal to 0.50 unless you use isEqualByComparingTo
. see https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#compareTo-java.math.BigDecimal-
example:
// assertion will pass because 8.0 is equal to 8.00 using BigDecimal.compareTo(BigDecimal)
assertThat(new BigDecimal("8.0")).isEqualByComparingTo(new BigDecimal("8.00"));
// assertion fails
assertThat(new BigDecimal("8.0")).isEqualTo(new BigDecimal("8.00"));