2

I have a small problem with Assertion, or maybe just with BigDecimal.

My JUnit test is throwing an error while using assertEquals(Object expected, Object actual):

java.lang.AssertionError: expected:<10> but was:<10.000000000>

expected is created via:

BigDecimal expected = MathHelper.getDecimal(10);

The getDecimal method in MathHelper looks like this:

public static final BigDecimal getDecimal(long value) {
    return BigDecimal.valueOf(value);
}

The actual is a private BigDecimal count and it's getter method is a classic getter:

public BigDecimal getCount() {
    return count;
}

I have absolutely no idea what is going on here...

hc0re
  • 1,806
  • 2
  • 26
  • 61

2 Answers2

3

Take a look at the documentation of the equals method for BigDecimal:

Compares this BigDecimal with the specified Object for equality. Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).

So no, 10 and 10.000000000 are not equeal, and the assertion error is correct.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
-1

BigDecimal equals method takes into account scale of the number, so BigDecimals 12.0 and 12.00 are different. You should use Bigdecimal compareTo method.

adushenin
  • 259
  • 1
  • 3
  • compareTo is returning an int equal to -1, 1 or 0. I would love to know the information what were the expected and actual values, not just the information that they differ... – hc0re Oct 31 '15 at 21:08