Replace the not (!) operator within the assertions with an appropriate assertion

Definition:

Instead of creating a conditional logic in the assertions using the not (!) operator, developers should choose the corresponding pair of the specific assert method. In more detail,

  • Developers should use assertFalse instead of the not (!) operator to negate a condition within the assertTrue,

  • Developers should use assertNotEquals instead of the not (!) operator to negate a condition within the assertEquals,

  • Developers should use assertNotNull instead of the not (!) operator to negate a condition within the assertNull.

(and vice-versa).

Example from practice:

The following code snippet present the refactoring for the testGenerateClientId test method of the Kafka project. The method uses an assertTrue method to check whether the ids.contains(id) condition is false (line 293, highlighted in red). The refactoring of the assertion consists of the:

  1. removal of the not (!) operator within the conditional expression,

  2. replace the assertTrue method with the assertFalse method (line 293, highlighted in green).

Replacing the *assertTrue* method with the *assertFalse* method