Search code examples
javaassertbigdecimal

Mockito and Assertions - two same BigDecimals... not equal


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...


Solution

  • 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.