JavaJUnit: 方法 X 对于 Y 类型是不明确的

我做了一些测试,效果很好。然后,我将它移动到另一个包中,现在出现了错误。密码如下:

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"));
}

错误消息如下:

对于 JGraphtUtilitiesTest 类型,assertEquals (Object,Object)方法是不明确的

我如何解决这个问题? 为什么当我将类移动到一个不同的包时会出现这个问题?

112671 次浏览

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());

You can use the method

assertEquals(double expected, double actual, double delta)

Which will take into account rounding error that are hinerent to floating point (see this post for example). You can write

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

This mean that as long as the two values differ for less than 0.0001 they are considered to be equals. This has two advantages:

  • Compares floating point values as they are supposed to
  • No need to cast, as the three argument assert only applyes to doubles, not to generic Objects

The simplest solution to this problem is just cast the second parameter into a primitive:

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

Ambiguity removed.

This is valid for any of the Number subclasses, for example:

assertEquals(70, (int)new Integer(70));

Would solve an ambiguity too.

However, assertEquals(double, double) is deprecated as of now and for good reasons, so I encourage you to use the method with a delta as others have suggested already.

By good reasons I mean that, given the inner representation of double numbers, two apparently equal double numbers can differ in an irrelevant infinitesimal fraction and wouldn't pass a test, but that doesn't mean that there's anything wrong with your code.