Why doesn't JUnit provide assertNotEquals methods?

后端 未结 11 1110
忘了有多久
忘了有多久 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:14

    I'd suggest you use the newer assertThat() style asserts, which can easily describe all kinds of negations and automatically build a description of what you expected and what you got if the assertion fails:

    assertThat(objectUnderTest, is(not(someOtherObject)));
    assertThat(objectUnderTest, not(someOtherObject));
    assertThat(objectUnderTest, not(equalTo(someOtherObject)));
    

    All three options are equivalent, choose the one you find most readable.

    To use the simple names of the methods (and allow this tense syntax to work), you need these imports:

    import static org.junit.Assert.*;
    import static org.hamcrest.CoreMatchers.*;
    

提交回复
热议问题