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

问题描述 投票:0回答:1

我使用另一个类减去了两个对象,但是当我返回新值并与期望值进行比较时,即使它是相同的值,也会产生错误(我认为)。

我尝试使用if语句,但有相同之处。

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

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

进口到该头等舱(最好不要更改)。>>

   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)));
}

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

在另一个文件上。进口

import java.math.BigDecimal;

我们有这些字段(第二类中的变量。]​​>

    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

我使用另一个类减去了两个对象,但是当我返回新值并与期望值进行比较时,即使它是相同的值,也会产生错误(我认为)。我尝试使用if语句...

java maven intellij-idea junit maven-plugin
1个回答
0
投票

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

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


0
投票

我不知道您拥有Money类的哪种实现,尤其是equals方法,但是您在此处将String与原始值进行比较:

© www.soinside.com 2019 - 2024. All rights reserved.