I am getting the following error:
Both assertEquals(Object, Object) in Assert and assertEquals(double, double) in Assert match
For this line of code in my Junit
tests, note that getScore
() returns a double
:
assertEquals(2.5, person.getScore());
This is my assert import:
import static org.junit.Assert.*;
What is causing this and how can I fix this?
The method assertEquals(Object, Object) is ambiguous for the type ... What this error means is that you're passing a double and and Double into a method that has two different signatures: assertEquals(Object, Object) and assertEquals(double, double) both of which could be called, thanks to autoboxing.
Yes, assertEquals() invokes the overridden equals() if the class has one. Show activity on this post. Yes, it calls equals and there is a separate method, assertSame , that uses == . Just to clear things up, assertEquals works with any object since all objects declare equals .
You want: assertEquals(42681241600L, 42681241600L); Your code was calling assertEquals(Object, Object). You also needed to append the 'L' character at the end of your numbers, to tell the Java compiler that the number should be compiled as a long instead of an int.
assertEquals. Asserts that two objects are equal. If they are not, an AssertionError without a message is thrown. If expected and actual are null , they are considered equal.
Your getScore() returns Double
, not double
. Therefore compiler is confused: Should it convert both arguments to Object, or if it should convert only the Double to double?
double a = 2.0; Double b = 2.0; // assertEquals(a,b); // fails to compile // the compiler is confused whether to use assertEquals((Object) a,(Object) b); // OK // or assertEquals(a,(double) b); // OK
Anyway, I would set the method to return primitive type double.
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