tags:

views:

3953

answers:

3

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

It provides assertNotSame (corresponding to assertSame) and assertFalse (corresponding to assertTrue), so it seems strange that they didn't bother including assertNotEqual.

By the way, I know that JUnit-addons provides the methods I'm looking for. I'm just asking out of curiosity.

+4  A: 

I wonder same, api of Assert is not too symmetric, because for testing do objects refer to same it provides assertSame and assertNotSame.

Of course it is not too long to write:

assertFalse(foo.equals(bar));
Mikko Maunu
+22  A: 

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.

Joachim Sauer
That's cool! I didn't know about those asserts. Thanks.
Chris B
I appreciate the pointer to the alternate assertion syntax, but pointing elsewhere doesn't answer *why* JUnit never provided `assertNotEquals()`.
seh
@seh: The way I read it the question was not about historical interest, but about a way to formulate the assertion "these two objects are not equal" in a JUnit test. I answered that. Considering the "why is/was there no `assertNotEqual`" I'd say that's because it's a specialized assert that's not needed as often as `assertEquals` and therefore would be expressed via the generic `assertFalse`.
Joachim Sauer
By the way, to use the assertions mentioned above, one must first import the literate symbols with a statement like `import static org.hamcrest.CoreMatchers.*`.
seh
A: 

thank you both for the suggestions, very interesting to explore

Sam