Folks, why I'm getting the "The method assertEquals(String, Object, Object) is ambiguous for the type DictionaryTest" error for this JUnit test?
@Test
public void testEditCard() {
Integer a = 10;
Integer b = 12;
Integer c = 2;
assertEquals("test", a-b, c);
}
Adding casting assertEquals("test", (Integer)(a-b), c);
resolves the problem.
Because of the wonders of autoboxing and -unboxing:
assertEquals("test", /* this is an int */ a-b, /* this is an Integer */ c);
Can be evaluated as
assertEquals(String, long, long);
// in this case the second parameter is unboxed
// (and the first one silently casted)
or as
assertEquals(String, Object, Object);
// in this case the first parameter is boxed
If you declare all variables as int (not Integer), there should be no ambiguity.
It's because the compiler can't tell if you want to call assertEquals(String, Object, Object)
or assertEquals(String, long, long)
. Since a-b
and c
can be automatically coerced to long
the compiler sees an ambiguity.
Your explicit cast tells the compiler that you want the Object version.
Note that in this case you could use int
rather than Integer
variables which would also fix the ambiguity.
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