对于String,建议使用断言的assertTrue()或assertEquals()的JUnit?

Gan*_*esh 9 java junit

我的代码如下

@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));
}
Run Code Online (Sandbox Code Playgroud)

我想知道哪种方法最好.

And*_*bbs 19

类型1是首选,因为当它们不匹配时您将收到断言消息.

org.junit.ComparisonFailure: expected: <[foo]> but was: <[bar]>
Run Code Online (Sandbox Code Playgroud)

VS

java.lang.AssertionError
Run Code Online (Sandbox Code Playgroud)