Java 不明确的方法调用 Assert 中的 assertEquals(Object, Object) 和 Assert 匹配中的 assertEquals(double, double) :

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41893633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:46:00  来源:igfitidea点击:

Ambiguous method call Both assertEquals(Object, Object) in Assert and assertEquals(double, double) in Assert match:

javaobjectjunitdoubleassert

提问by java123999

I am getting the following error:

我收到以下错误:

Both assertEquals(Object, Object) in Assert and assertEquals(double, double) in Assert match

For this line of code in my Junittests, note that getScore() returns a double:

对于我Junit测试中的这行代码,请注意getScore() 返回一个double

assertEquals(2.5, person.getScore());

This is my assert import:

这是我的断言导入:

import static org.junit.Assert.*;

What is causing this and how can I fix this?

这是什么原因造成的,我该如何解决?

采纳答案by Bechyňák Petr

Your getScore() returns Double, not double. Therefore compiler is confused: Should it convert both arguments to Object, or if it should convert only the Double to double?

您的 getScore() 返回Double,而不是double。因此编译器很困惑:它应该将两个参数都转换为 Object,还是应该只将 Double 转换为 double?

    double a = 2.0;
    Double b = 2.0;
    // assertEquals(a,b); // fails to compile
    // the compiler is confused whether to use
    assertEquals((Object) a,(Object) b); // OK
    // or
    assertEquals(a,(double) b); // OK

Anyway, I would set the method to return primitive type double.

无论如何,我会将方法设置为返回原始类型 double。

回答by M. Prokhorov

If you specifically interested in using Assert.assertEquals(double, double)(the primitive version), try calling overridden method that allows deviation and setting allowed deviation to zero, like this:

如果您对使用Assert.assertEquals(double, double)(原始版本)特别感兴趣,请尝试调用允许偏差并将允许偏差设置为零的覆盖方法,如下所示:

assertEquals(2.5, person.getScore(), 0.0);

You might also want to have third parameter to be something other than zero if person.getScore()is allowed to be slightly different from 2.5. For example, if 2.500001is acceptable, then your test becomes

如果person.getScore()允许与2.5. 例如,如果2.500001可以接受,那么您的测试将变为

assertEquals(2.5, person.getScore(), 0.000001);

回答by Molten Ice

If you specifically want to avoid casting AND use the primitive version, you can get the primitive result from a wrapper object. For example:

如果您特别想避免强制转换并使用原始版本,则可以从包装器对象中获取原始结果。例如:

    double a = 2.0;
    Double b = 2.0;
    assertEquals(a, b.doubleValue()); //Deprecated so use the one with delta

    Integer c = 2;
    int d = 2;
    assertEquals(c.intValue(), d);

    Long e = 2L;
    long f = 2L;
    assertEquals(e.longValue(), f);

回答by JavaSheriff

I had the same error, I changed from this:

我有同样的错误,我改变了这个:

assertEquals("Server status code is: " +  wmResp.getStatusCode() , 200, wmResp.getStatusCode());

To this

对此

assertEquals("Server status code is: " +  wmResp.getStatusCode() , new Integer(200), wmResp.getStatusCode());

This is happening because the first line compiler takes the 200 as primitive (integer not Integer class)

发生这种情况是因为第一行编译器将 200 作为原始值(整数而不是整数类)