Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous method call Both assertEquals(Object, Object) in Assert and assertEquals(double, double) in Assert match:

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?

like image 383
java123999 Avatar asked Jan 27 '17 12:01

java123999


People also ask

Is ambiguous for the type assert?

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.

Can assertEquals compare objects?

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 .

How do you assert long value in Junit?

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.

How does junit assertEquals work?

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.


1 Answers

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.

like image 132
Bechyňák Petr Avatar answered Sep 19 '22 11:09

Bechyňák Petr