Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit which is recommended assertTrue() or assertEquals() for String?

Tags:

java

junit

My code is like below

@Test
public void testMyMethod(){
    MyClass mc = new MyClass();
    String exeVal="sometext some text";
    String x=mc.exampleMethod();

    // Assertion type 1
    Assert.assertEquals(exeVal,x);
    //Assertion Type 2
    Assert.assertTrue(exeVal.equals(x));
}

I want to know which is the best approach.

like image 201
Ganesh H Avatar asked Jul 18 '14 09:07

Ganesh H


Video Answer


1 Answers

Type 1 is preferred because of the assertion message you will receive when they do not match.

org.junit.ComparisonFailure: expected: <[foo]> but was: <[bar]>

vs

java.lang.AssertionError
like image 136
Andrew Stubbs Avatar answered Oct 26 '22 15:10

Andrew Stubbs