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?
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.
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.
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); }
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With