Search code examples
javaeclipsetestingjunitassert

JUnit test difference between assertEquals() and Assert.assertEquals()


I made a method to count occurrence of a given character in 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.

I googled and I found that to test it I needed use:

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

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

Could you explain me why? What is the difference?


Solution

  • You don't provide any details for this:

    Eclipse complains it.

    I suspect it is an Ambiguous method call ...

    enter image description here

    ... which is caused by there being multiple 'forms' of assertEquals some of which take int, some long, some Object, some String etc etc.

    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:

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