Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous method while testing

I'm trying to test the method calculate from MyClass:

// TestClass {
@Test
public void testCalculate() {
    MyClass tester = new MyClass();
    assertEquals((long)123, tester.calculate().get(5));
}

// MyClass
public ArrayList<Long> calculate() {} // signature

Unfortunately I get the following error:

The method assertEquals(Object, Object) is ambiguous for the type TestClass

What am I doing wrong? The return type of calculate is an ArrayList with long-values and I expect the long number 123.

When I do the following, it works:

// TestClass {
@Test
public void testCalculate() {
    MyClass tester = new MyClass();
    ArrayList<Long> arr = new ArrayList<Long>();
    arr.add((long) 123);
    assertEquals(arr.get(0), tester.calculate().get(5));
}
like image 928
Evgenij Reznik Avatar asked Nov 27 '25 05:11

Evgenij Reznik


1 Answers

Your call to assertEquals has one argument of type long and the other of type Long. Use one of the following:

assertEquals(Long.valueOf(123L), tester.calculate().get(5));

or

assertEquals(123L, tester.calculate().get(5).longValue());

(I suggest using a long literal 123L rather than a cast to a long of an int literal.)

like image 153
Ted Hopp Avatar answered Nov 29 '25 17:11

Ted Hopp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!