Why doesn't JUnit provide assertNotEquals methods?

后端 未结 11 1104
忘了有多久
忘了有多久 2020-12-12 08:30

Does anybody know why JUnit 4 provides assertEquals(foo,bar) but not assertNotEqual(foo,bar) methods?

It provides assertNotSame

11条回答
  •  抹茶落季
    2020-12-12 09:17

    I wonder same. The API of Assert is not very symmetric; for testing whether objects are the same, it provides assertSame and assertNotSame.

    Of course, it is not too long to write:

    assertFalse(foo.equals(bar));
    

    With such an assertion, the only informative part of the output is unfortunately the name of the test method, so descriptive message should be formed separately:

    String msg = "Expected <" + foo + "> to be unequal to <" + bar +">";
    assertFalse(msg, foo.equals(bar));
    

    That is of course so tedious, that it is better to roll your own assertNotEqual. Luckily in future it will maybe be part of the JUnit: JUnit issue 22

提交回复
热议问题