Does anyone know why I\'m getting this error when it is set as ints in my classe
ID: 3817376 • Letter: D
Question
Does anyone know why I'm getting this error when it is set as ints in my classes, and if I take away the .01 it fails. Can someone help?
Explanation / Answer
As per the error trace shown, the issue is that yoou are trying to assign a double value to the int value . Here is the detailed explanation of your issue: -
method Assert.assertEquals has following signature: -
assertEquals(double expected, double actual, double delta) i.e it holds three parameters, expected actual and the acceptable difference between them, i.e. the method will throw AssertionError if, expected and actual DOUBLE is not equal and even not within the possible delta.
So this method accepts all three doubles. There is no issue with 0.01 you have in your call. as it is double only.
The issue is with expected/actual value. while calling the method, you have supplied two integers in place of double. which is ok, because double holds higher precedence than int, So int can be stored/cast into double easily and this task is done automatically by compiler without lossing precision. i.e. expected value 7 will be casted to 7.0 to be sent to the method for execution.
Now here comes the problem. Your test case is expecting int value , not double. As int has lesser precedence than double, each double needs to be type casted manually because there is loss of precision involved. Loss of precision means if double is 4.23, than int will take 4 and ignore 0.23.
As both your expected and actual were casted to double by compiler(because of assertEquals), you have to now manually type cast to int for test case to work. But you are not doing this. So this error is coming.
So please take care of type casting in your test cases whereever you need to store double in int . The program will work without errors then.
Sample type casting for you is as follows: -
int testVar = (int)wc.getTiers();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.