4

I would like to compare double values with AssertJ. I dont catch why my test is failing.

@Test
public void testInterestToQuote() {
    double result = BasicCalculator.accumulationFactorByYearsAndInterest(years, interest)
    Assertions.assertThat(result).isCloseTo(expected, Assertions.offset(0.1d))
}

Exception is:

java.lang.AssertionError: 
Expecting:
   <7.256571590148141E-5>
to be close to:
   <7.25>
by less than <0.1> but difference was <7.249927434284099>.
(a difference of exactly <0.1> being considered valid)

Why does Assertion fail?

Michael Hegner
  • 5,555
  • 9
  • 38
  • 64

2 Answers2

1

It was late yesterday, but 7.256571590148141E-5 is not as I thought, 7.25.., because E-5 moves the point 5 to the left, so it is 0.0000725...

Michael Hegner
  • 5,555
  • 9
  • 38
  • 64
  • 9
    In case somebody else is looking for assertj decimal point comparison with a certain precision degree and ends up here. The correct way to do that would be something like: `assertThat(result).isEqualTo(expected, withPrecision(2d))` – atavio Sep 09 '20 at 12:35
0

ad assertj decimal point comparison with Double/Float:

In my case I was comparing Double to Float

assertThat(resultDouble).isEqualTo(expectedFloat);

-> assertThat(resultDouble).isEqualTo(expectedDouble);

electrobabe
  • 1,549
  • 18
  • 17