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

I get the following error:

Both assertEquals(Object, Object) in Assert and assertEquals(double, double) in Assert match

For this line of code in my test, Junitnote that getScore() returns double:

assertEquals(2.5, person.getScore());

This is my correct import:

import static org.junit.Assert.*;

What causes this and how can I fix it?

+4
source share
2 answers

I think your getScore () returns Double, not double. So the compiler is confused: should it convert both arguments to Object, or should it only convert 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

In any case, I would set a method to return the primitive type double.

+9

Assert.assertEquals(double, double) ( ), , , :

assertEquals(2.5, person.getScore(), 0.0);

, , - , person.getScore() 2.5. , 2.500001 ,

assertEquals(2.5, person.getScore(), 0.000001);
+1

Source: https://habr.com/ru/post/1667982/


All Articles