0

Okay, so I'm doing an homework and I have to create an object called Bonus.

And I have to do a test. In that case, I'm doing testing [getBonusPercent]

But yet, it tell it doesn't work because The method assertEquals(double, double) from the type Assert is deprecated.

I don't understand what am I doing wrong. This should worked fine

Also, sorry for my broken English.

I tried other Assert but so far, nothing work. Also, i have import static [org.junit.Assert.assertEquals;] wrote in the script

In the main methode

private float bonusPercentage;

Bonus(float bonus) {
  this.bonusPercentage = bonus;
}

public float getBonusPercent() {
  return this.bonusPercentage;
}

The test

this.bonus = new Bonus(BONUS);

assertEquals(BONUS, this.bonus.getBonusPercent());

assertEquals(double, double) from the type Assert is deprecated.

adiga
  • 34,372
  • 9
  • 61
  • 83
  • 1
    javascript is not java – adiga Aug 31 '19 at 13:46
  • Use the variant that accepts a delta. Else the comparison might fail just due to limited double-precision. You can use a small delta like `0.00001` of course. – Zabuzard Aug 31 '19 at 13:50
  • See [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) to understand why this is necessary and why that method is deprecated. – Zabuzard Aug 31 '19 at 13:53

3 Answers3

1

You will have to use assertEquals(BONUS, this.bonus.getBonusPercent(), 0.01d); This will make sure, that the comparation does not fail in some cases where double is not precise enough.

Halko Karr-Sajtarevic
  • 2,248
  • 1
  • 16
  • 14
1

When you find a deprecated method, you have to look in the docs for the proposed alternative. In this case, you will find that you have to use Assert.equals(double, double, double) where the last double is the delta, or the maximum amount of difference allowed to still be considered equal, as per the specification in the docs

Deprecated method with proposed alternative

tinker
  • 1,396
  • 11
  • 20
0

The assertEquals(double, double) is deprecated because a newer version of it, assertEquals(double, double, double) should be used.

The thrid parameter here is a delta value. It indicates an acceptable difference between the 2 given double values.

Simply set the third parameter to 0 to check if the numbers are equal, or use a small delta value.

Gtomika
  • 845
  • 7
  • 24
  • 2
    Do not use `0`. It happens so quick that something like `0.2 == 0.1 + 0.1` fails just because of double precision letting it end up at `0.199999999`. Use a small delta like `0.00001`. – Zabuzard Aug 31 '19 at 13:51
  • See [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) to understand why this is necessary and why that method is deprecated. – Zabuzard Aug 31 '19 at 13:53
  • 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. – Zabuzard Aug 31 '19 at 13:58