简体   繁体   English

assertEquals()和Assert.assertEquals()之间的JUnit测试差异

[英]JUnit test difference between assertEquals() and Assert.assertEquals()

I made a method to count occurrence of a given character in String. 我制作了一种方法来计算String中给定字符的出现。

public Integer numberOf(String str, Character a){}

I tried to test as normal using: 我尝试使用以下方法进行正常测试:

@Test
public void test1(){
    Integer result = oc.numberOf("Lungimirante", 'u');
    Assert.assertEquals(1, result);
}

but Eclipse complains it. 但是Eclipse抱怨。

I googled and I found that to test it I needed use: 我用谷歌搜索,发现要测试它,我需要使用:

assertEquals(1, result); //it works correctly

instead of: Assert.assertEquals(1, result); 而不是: Assert.assertEquals(1, result);

Could you explain me why? 您能解释一下为什么吗? What is the difference? 有什么区别?

You don't provide any details for this: 您没有为此提供任何详细信息:

Eclipse complains it. Eclipse抱怨它。

I suspect it is an Ambiguous method call ... 我怀疑这是一个Ambiguous method call ...

在此处输入图片说明

... which is caused by there being multiple 'forms' of assertEquals some of which take int, some long, some Object, some String etc etc. ...是由assertEquals多个“形式”引起的,其中的一些形式为int,一些long,一些Object,一些String等。

So, you just need to be explicit about which one you want to use. 因此,您只需要明确说明要使用哪一个即可。 For example, both of the following assertEquals calls compile because they are explicit about the type of the expected and actual argument: 例如,以下两个assertEquals调用均会编译,因为它们对expected参数和actual参数的类型都是明确的:

Integer result = oc.numberOf("Lungimirante", 'u');
Assert.assertEquals(new Integer(1), result);
Assert.assertEquals(1, result.intValue());

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

相关问题 Assert.assertEquals在两个列表上 - Assert.assertEquals on two lists Assert.assertEquals的奇怪行为 - Strange behaviour of Assert.assertEquals 如何使用 Assert.assertEquals() 测试单链表 - How to test Single Linked List using Assert.assertEquals() Selenium可以对测试(Assert.assertEquals())失败进行截图吗? - Can Selenium take a screenshot on test(Assert.assertEquals()) failure? 如果在Assert.assertEquals硒测试NG上有其他条件 - if else condition on Assert.assertEquals selenium testNG assertEquals(Double, Double) 和 assertEquals(double, double, delta) 之间的 Junit 区别 - Junit difference between assertEquals(Double, Double) and assertEquals(double, double, delta) Selenium Web驱动程序-Java-TestNG-Assert.assertEquals如何将实际结果与预期结果之间的范围进行比较 - Selenium Web Driver - Java - TestNG - Assert.assertEquals how to compare actual result with the range between expected result Assert.assertEquals 没有正确比较 String - Assert.assertEquals didn't compares String right way Grails / groovy测试-assert和assertEquals方法之间的任何区别 - Grails/groovy testing - any difference between assert and assertEquals methods java-有时返回字符串且有时返回null时如何使用Assert.assertEquals - java - How to use Assert.assertEquals when sometimes a String is returned and sometimes null is returned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM