1. I suggest the new assertThat() style assertions, which makes it easy to describe various negations and automatically generates a description of what you expect if the assertions fail: assertThat(objectUnderTest,
is(not(someOtherObject))); assertThat(objectUnderTest, not(someOtherObject)); assertThat(objectUnderTest, not(equalTo(someOtherObject)));
All three options are equivalent, pick the one you find most readable (and allow this tight syntax to work), you need these imports:
i
mport static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*;
2. There is assertNotEquals in JUnit 4.11:
3. I don't know that the asserts API is not very suitable for testing the objects it provides and of course CodeGo.net, is this too Long write:
assertFalse(foo.equals(bar));
With the yield part this argument is so unfortunate it should be respectively:
String msg = "Expected <" + foo + "> to be unequal to <" + bar +">"; assertFalse(msg, foo.equals(bar));
That's of course makes it better to launch yourself assertNotEqual . Fortunately, in the future it will probably become part of JUnit: JUnit Issue 22
4. I think it's a neat thing to keep in mind if assertNotEqual does make JUnit a little less interesting to learn. In the case when adding it would reduce the API and at least help to govern the larger space. My guess is that the reason for the omission might be because there are too few multiplayer calls. However, I get one even when assertFalse doesn't exist. So there's a positive expectation that I might end up adding it, since it's not a difficult one, although I admit that there are workarounds that are even elegant. I'm very late to this party, though. I found that the following format:
static void assertTrue(java.lang.String message, boolean condition)
int status = doSomething() ; // expected to return 123 assertTrue("doSomething() returned unexpected status", status != 123 ) ;
7. The reason is that one would expect assertNotEquals() to be a built-in command without converting them first. For fully mature objects: Detailed examples:
.... assertThat(1, not(equalTo(Integer.valueOf(winningBidderId)))); ....
assertNotEqual(1, winningBidderId);