如果没有第二个参数转换,assertEquals 将不起作用
各位,为什么我在这个 JUnit 测试中收到“方法 assertEquals(String, Object, Object) 对于类型 DictionaryTest 来说不明确”错误?
@Test
public void testEditCard() {
Integer a = 10;
Integer b = 12;
Integer c = 2;
assertEquals("test", a-b, c);
}
添加强制转换 assertEquals("test", (Integer)(ab), c);
可以解决该问题。
Folks, why I'm getting the "The method assertEquals(String, Object, Object) is ambiguous for the type DictionaryTest" error for this JUnit test?
@Test
public void testEditCard() {
Integer a = 10;
Integer b = 12;
Integer c = 2;
assertEquals("test", a-b, c);
}
Adding casting assertEquals("test", (Integer)(a-b), c);
resolves the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于自动装箱和拆箱的奇迹:
可以计算为
或为
如果将所有变量声明为 int (而不是 Integer),那么应该不会有歧义。
Because of the wonders of autoboxing and -unboxing:
Can be evaluated as
or as
If you declare all variables as int (not Integer), there should be no ambiguity.
这是因为编译器无法判断您是否要调用
assertEquals(String, Object, Object)
还是assertEquals(String, long, long)
。由于ab
和c
可以自动强制为long
,因此编译器会出现歧义。您的显式转换告诉编译器您需要对象版本。
请注意,在这种情况下,您可以使用
int
而不是Integer
变量,这也可以解决歧义。It's because the compiler can't tell if you want to call
assertEquals(String, Object, Object)
orassertEquals(String, long, long)
. Sincea-b
andc
can be automatically coerced tolong
the compiler sees an ambiguity.Your explicit cast tells the compiler that you want the Object version.
Note that in this case you could use
int
rather thanInteger
variables which would also fix the ambiguity.