简体   繁体   English

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

[英]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 方法assertEquals(Object,Object)对于JGraphtUtilitiesTest类型是不明确的

How can I fix this? 我怎样才能解决这个问题? Why did this problem occur as I moved the class to a different package? 当我将类移到另一个包时,为什么会出现此问题?

The method assertEquals(Object, Object) is ambiguous for the type ... assertEquals(Object,Object)方法对于类型是不明确的......

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. 这个错误的含义是你将doubleDouble传递给一个有两个不同签名的方法: assertEquals(Object, Object)assertEquals(double, double)这两个都可以被调用,这要归功于自动装箱。

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). 为避免歧义,请确保调用assertEquals(Object, Object) (通过传递两个Doubles)或assertEquals(double, double) (通过传递两个双精度)。

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. 这意味着只要两个值的差异小于0.0001,它们就被认为是等于。 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: 这适用于任何Number子类,例如:

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. 但是,assertEquals(double,double)现在已被弃用,并且有充分的理由,因此我鼓励您使用带有delta的方法,正如其他人已经建议的那样。

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. 由于充分的理由,我的意思是,给定双数的内部表示,两个明显相等的双数可以在无关的无穷小部分中有所不同并且不会通过测试,但这并不意味着您的代码有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM