为什么使用 if 语句或 assertj 比较相同的对象值总是出错?

Why is comparing same objects values using if statement or assertj always gives error?

我使用另一个 class 减去两个对象,但是当我 return 新值并与预期值进行比较时,即使它是相同的值(我认为)也会出错。

我尝试使用 if 语句,但同样如此。

   if(expected.current_Money.toString().equals(0.5)){
        System.out.println(" if statmenet is working...");

    }
    else
    {
        System.out.println("failed");
    }

首先导入 class(最好不要更改它)。

   import org.assertj.core.api.ThrowableAssert;
   import org.junit.jupiter.api.Test;
   import java.math.BigDecimal;

   import static org.assertj.core.api.Assertions.assertThat;
   import static org.assertj.core.api.Assertions.assertThatThrownBy;

同一个文件,我们有这个方法。

@Test
void subtract_two_money_should_be_correct() {
    Money oneDinar = new Money(BigDecimal.valueOf(1));
    Money halfDinar = new Money(BigDecimal.valueOf(0.5));
    System.out.println("Value of oneDinar 
    "+oneDinar.current_Money.toString());
    System.out.println("Value of halfDinar 
    "+halfDinar.current_Money.toString());

    //Money expected = oneDinar.subtract(halfDinar);
    Money expected = new Money(BigDecimal.valueOf(0.5));

    System.out.println("the Value of Expected 
    "+expected.current_Money.toString());

    assertThat(expected).isEqualTo(new Money(BigDecimal.valueOf(0.5)));
}

subtracted 的输出被保存到一个对象中,并且内部变量的数据类型为 BigDecimal。

在另一个文件上。 进口

import java.math.BigDecimal;

我们有这些字段(第二个中的变量class。

    public static final Money HALF_DINAR = new 
  Money(BigDecimal.valueOf(0.5));


    public static final Money QUARTER_DINAR = new 
  Money(BigDecimal.valueOf(0.25)) ;
    public static final Money DINAR = new Money(BigDecimal.valueOf(1));
    public BigDecimal current_Money;

这是最后一件事,我们用来减去的方法。

  public Money subtract(Money SubtractMoney) {
            System.out.println("TheValue of current money "+current_Money.toString());
            Money current = new Money(current_Money);
            System.out.println("TheValue of current  "+current.current_Money.toString());

            BigDecimal subtractedNumber = current_Money.subtract(new BigDecimal(String.valueOf(SubtractMoney.current_Money)));
            System.out.println("TheValue of subtractedNumber"+subtractedNumber.toString());

            Money newMoney = new Money(subtractedNumber);
            System.out.println("TheValue of newMoney "+newMoney.current_Money.toString());

            return newMoney ;
        }

我希望输出与该对象相同(在数字上是相同的,但即使我使用 if 语句或 Assertthat,它也会给我错误。

我希望输出是真实的并且没有错误。 数字也是 0.5,这是目标。

输出:

Value of oneDinar 1
Value of halfDinar 0.5
TheValue of current money 1
TheValue of current  1
TheValue of subtractedNumber0.5
TheValue of newMoney 0.5
the Value of Expected 0.5

org.opentest4j.AssertionFailedError: 
Expecting:
 <com.progressoft.induction.Money@123f1134>
to be equal to:
 <com.progressoft.induction.Money@7d68ef40>
but was not.
Expected :com.progressoft.induction.Money@7d68ef40
Actual   :com.progressoft.induction.Money@123f1134

AssertJ的isEqualTo(...)函数在内部使用对象的equals方法来判断是否相等。因此,您应该覆盖 Money class 中的 equals() 方法。然后您可以通过内容而不是对象标识来比较实例,即如果值相同,则实例被认为是相等的。

您的 if (expected.current_Money.toString().equals(0.5)) 语句中的比较不起作用,因为 expected.current_Money.toString() 部分是字符串,而 0.5 被自动装箱到 Double 对象。因此,等于比较失败,因为 classes 不同。

旁注:无论何时覆盖 equals() 方法,您还需要覆盖 hashcode() 函数以匹配 equals() 函数。否则许多 classes(例如 HashMap)可能会行为不端,因为他们假设 equals 和 hashcode 基于相同的假设工作。