Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JUnit: The method X is ambiguous for type Y

I had some tests working fine. Then, I moved it to a different package, and am now getting errors. Here is the code:

import static org.junit.Assert.*; import java.util.HashSet; import java.util.Map; import java.util.Set;  import org.jgrapht.Graphs; import org.jgrapht.WeightedGraph; import org.jgrapht.graph.DefaultWeightedEdge; import org.jgrapht.graph.SimpleWeightedGraph; import org.junit.*;   @Test     public void testEccentricity() {         WeightedGraph<String, DefaultWeightedEdge> g = generateSimpleCaseGraph();         Map<String, Double> eccen = JGraphtUtilities.eccentricities(g);          assertEquals(70, eccen.get("alpha"));         assertEquals(80, eccen.get("l"));         assertEquals(130, eccen.get("l-0"));         assertEquals(100, eccen.get("l-1"));         assertEquals(90, eccen.get("r"));         assertEquals(120, eccen.get("r-0"));         assertEquals(130, eccen.get("r-1"));     } 

The error message is this:

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

How can I fix this? Why did this problem occur as I moved the class to a different package?

like image 751
Nick Heiner Avatar asked Nov 28 '09 00:11

Nick Heiner


People also ask

What is ambiguous method?

This ambiguous method call error always comes with method overloading where compiler fails to find out which of the overloaded method should be used. Suppose we have a java program like below.

What does ambiguous mean in java?

The ambiguities are those issues that are not defined clearly in the Java language specification. The different results produced by different compilers on several example programs support our observations.

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.


1 Answers

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.

To avoid the ambiguity, make sure that you either call assertEquals(Object, Object) (by passing two Doubles) or assertEquals(double, double) (by passing two doubles).

So, in your case, you should use:

assertEquals(Double.valueOf(70), eccen.get("alpha")); 

Or:

assertEquals(70.0d, eccen.get("alpha").doubleValue()); 
like image 142
Pascal Thivent Avatar answered Oct 01 '22 03:10

Pascal Thivent