Java JUnit: method X is ambiguous for type Y

I had some tests working fine. Then I moved it to another package and now I get 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")); } 

Error message:

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

How can i fix this? Why did this problem happen when I moved the class to another package?

+69
java junit package compiler-errors testing
Nov 28 '09 at 0:21
source share
2 answers

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

This error means that you pass double and and double to a method that has two different signatures: assertEquals(Object, Object) and assertEquals(double, double) , which can be called due to autoboxing.

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

So in your case you should use:

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

Or:

 assertEquals(70.0d, eccen.get("alpha").doubleValue()); 
+167
Nov 28 '09 at 0:46
source share

You can use the method

 assertEquals(double expected, double actual, double delta) 

Which will take into account the rounding error, which is small for a floating point (for example, this post ). You can write

 assertEquals(70, eccen.get("alpha"), 0.0001); 

This means that while the two values ​​differ by less than 0.0001, they are considered equal. This has two advantages:

  • Compares floating point values ​​as they should
  • No need to drop, because the three arguments are only valid for doubles, not for shared objects
0
Apr 25 '17 at 9:08 on
source share



All Articles