Java JUnit:方法X对于类型Y是不明确的 [英] Java JUnit: The method X is ambiguous for type Y

查看:394
本文介绍了Java JUnit:方法X对于类型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"));
    }

错误讯息是:

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

如何解决这个问题?为什么会发生这个问题,因为我将类移动到不同的包?

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

推荐答案

方法assertEquals(Object,Object)对于类型...

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

这个错误意味着你传递一个 double Double 添加到具有两种不同签名的方法中: assertEquals(Object,Object) assertEquals(double,double

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

或:

assertEquals(70.0d, eccen.get("alpha").doubleValue());

这篇关于Java JUnit:方法X对于类型Y是不明确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆