95

I was wondering why assertEquals(double, double) is deprecated.

I used import static org.junit.Assert.assertEquals; and I used JUnit 4.11.

Below is my code:

import org.junit.Test;
import static org.junit.Assert.assertEquals;


public class AccountTest {

@Test
public void test() {
    Account checking = new Account(Account.CHECKING);
    checking.deposit(1000.0);
    checking.withdraw(100.0);
    assertEquals(900.0, checking.getBalance());
   }
}

checking.getBalance() returns a double value.

What could be wrong?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
jsh6303
  • 2,010
  • 3
  • 24
  • 50

4 Answers4

115

It's deprecated because of the double's precision problems.

If you note, there's another method assertEquals(double expected, double actual, double delta) which allows a delta precision loss.

JavaDoc:

Asserts that two doubles are equal to within a positive delta. If they are not, an AssertionError is thrown. If the expected value is infinity then the delta value is ignored.NaNs are considered equal: assertEquals(Double.NaN, Double.NaN, *) passes

...

delta - the maximum delta between expected and actual for which both numbers are still considered equal.

Community
  • 1
  • 1
Codebender
  • 14,221
  • 7
  • 48
  • 85
  • 1
    @JiajuShen, it depends on your calculations... Let's say if you do `5.1 + 0.1`, you would expect `5.2` but the output would be `5.1999...`. So the delta can be `0.0001`... Or even lesser... – Codebender Oct 22 '15 at 05:52
  • 1
    Check out this [example](https://github.com/junit-team/junit/blob/3e43555e1f4df95b0a239f453af6d3226a8fef6e/src/test/java/org/junit/tests/assertion/AssertionTest.java#L285) from the JUnit source code or this [example](https://github.com/junit-team/junit/blob/3e43555e1f4df95b0a239f453af6d3226a8fef6e/src/test/java/junit/tests/framework/DoublePrecisionAssertTest.java) – Eric Oct 22 '15 at 06:03
28

People explain but don't give samples... So here goes what worked for me:

@Test
public void WhenMakingDepositAccountBalanceIncreases() {
    Account account = new Account();
    account.makeDeposit(10.0);
    assertEquals("Account balance was not correct.", 10.0, account.getBalance(), 0);
}

The 0 in the end;

Victor Augusto
  • 2,406
  • 24
  • 20
  • 15
    Using 0.0 as the delta is the same as using the deprecated method. The delta is intended to reflect how close the numbers can be and still be considered equal. Use values like 0.1 or 0.01 or 0.001, etc, depending on how much error the application can tolerate. – downeyt Jun 28 '17 at 15:25
  • 3
    This is a bad advise. Using `0` completely defeats the purpose of using the not-deprecated method. It is exactly the same as if using the deprecated variant. This is even more dangerous as you do not get a warning anymore. Acknowledge the reason why it was deprecated and use a small delta like `0.00001`. – Zabuzard Aug 31 '19 at 14:00
22

assertEquals(double, double) is deprecated because the 2 doubles may be the same but if they are calculated values, the processor may make them slightly different values.

If you try this, it will fail: assertEquals(.1 + .7, .8). This was tested using an Intel® processor.

Calling the deprecated method will trigger fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers"); to be called.

Eric
  • 6,563
  • 5
  • 42
  • 66
2

Old question but this hasn't been said yet and might help someone.

You can use com.google.common.math.DoubleMath.fuzzyEquals(double a, double b, double tolerance) which allows you to specify how close the two doubles should be to each other.

I found it very handy for unit tests where I don't want to hardcode test result values with a lot of decimal places.

steven35
  • 3,747
  • 3
  • 34
  • 48