liveBook · Manning

assertNotEquals

As explained in 100 Java Mistakes and How to Avoid Them

assertNotEquals is a test assertion that succeeds when two supplied values are considered unequal according to the testing framework's equality semantics.

assertNotEquals succeeds when two supplied values are considered unequal, but that description hides an important ambiguity: which direction of equals() is tested? Equality-contract tests can therefore pass even when equals() is badly implemented. In TestNG 7.4.0, assertNotEquals is effectively the opposite of assertEquals: it succeeds when either directional comparison is false, or when one value is null. section 10.6

A weak equality test

assertNotEquals(new MyCustomClass(), ""); class MyCustomClass { @Override public boolean equals(Object obj) { return true; } } This test can still pass because String.equals(MyCustomClass) returns false, even if MyCustomClass.equals(String) incorrectly returns true. A null argument can also avoid calling equals() entirely. section 10.6

A malformed call

assertNotEquals("Check that content was modified", storage.getContent()); When the intended call is assertNotEquals("Check that content was modified", oldContent, storage.getContent()), the two-argument form may treat the message as a value. The assertion then tests the wrong fact and may always pass unless the content equals that string. section 10.2

Avoid treating a passing negative assertion as strong evidence.

  • Prefer a positive, specific assertion that establishes the expected value rather than merely ruling out one value. section 10.2
  • Verify every assertion by deliberately seeing it fail; test-driven development makes this check explicit. section 10.2
  • Use static analysis as supporting evidence: IntelliJ IDEA can detect some obviously non-null assertion arguments, but not every malformed or weak assertion. section 10.2

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Defines inequality ambiguity, traces directional equals() calls and null handling, and stops short of implementing framework internals.
Examples
Uses malformed message arguments, custom equals() implementations, null values, strings, and content-change assertions.
What it assumes
Assumes familiarity with Java equals(), overloaded assertion signatures, and basic unit-test construction.
What you can do after
Diagnose weak or malformed inequality assertions and replace them with specific checks that demonstrably fail when broken.
How it connects
Connects assertNotEquals with equals(), equality contracts, unit testing, test-driven development, static analysis, and assertEquals.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.