assertEquals(obj,obj) returns a failed test

前端 未结 2 1868
心在旅途
心在旅途 2020-12-12 06:22

Hmm, I have a money object that allows me to add other money objects into it. I tried assertEquals() in java for testing out if my code if okay, but then it fai

相关标签:
2条回答
  • 2020-12-12 06:33

    You could write your tests to compare fields:

    Money m1 = new Money(money1.getCurrency(),new Value(22,70));
    Money m2 = new Money(money1.getCurrency(),new Value(22,70)).add(money2);
    
    assertEquals("currencies differ", m1.getCurrency(), m2.getCurrency());
    assertEquals("values differ",  m1.getValue(), m2.getValue());
    
    0 讨论(0)
  • 2020-12-12 06:47

    You didn't override equals() method from Object class in your Money class. If so, objects are compared by their references, which are different in this case. Here you can find rules for implementing equals.

    0 讨论(0)
提交回复
热议问题