Simplify Boolean Test Assertions

ID

java.test_assertion_boolean_simplify

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Java

Tags

best-practice, junit, testing

Description

Reports assertTrue(!expr) calls that should be simplified to assertFalse(expr), and assertFalse(!expr) calls that should be simplified to assertTrue(expr).

Rationale

Using a negated expression inside assertTrue or assertFalse is unnecessarily complex. When the assertion fails, the error message is less informative because the negation obscures the intent. Using the complementary assertion method directly makes the test intent clear and improves failure diagnostics.

// Bad - negated assertion
assertTrue(!result.isEmpty());
assertFalse(!isValid);

// Bad - with message
assertTrue("should be empty", !list.isEmpty());

Remediation

Replace assertTrue(!expr) with assertFalse(expr) and assertFalse(!expr) with assertTrue(expr).

// Good - direct assertion
assertFalse(result.isEmpty());
assertTrue(isValid);

// Good - with message
assertFalse("should be empty", list.isEmpty());