Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is assertEquals(double,double) deprecated in JUnit?

Tags:

java

junit

junit4

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?

like image 808
jsh6303 Avatar asked Oct 22 '15 05:10

jsh6303


People also ask

Why is assertEquals double deprecated?

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.

How many parameters does assertEquals () have?

Procedure assertEquals has two parameters, the expected-value and the computed-value, so a call looks like this: assertEquals(expected-value, computed-value); Note the order of parameters: expected-value and computed-value. Therefore, do not write the first call this way: assertEquals(C.

What is Delta in assertEquals?

delta - the maximum delta between expected and actual for which both numbers are still considered equal. It's probably overkill, but I typically use a really small number, e.g. private static final double DELTA = 1e-15; @Test public void testDelta(){ assertEquals(123.456, 123.456, DELTA); }

What is the method assertEquals () used for?

assertArrayEquals. Asserts that two object arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null , they are considered equal.


1 Answers

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.

like image 107
Codebender Avatar answered Sep 20 '22 18:09

Codebender