AssertJ Context Method After The Assertion

ID

java.assertj_context_before_assertion

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

assertion, reliability, testing

Description

Reports an AssertJ chain that ends in a description/configuration method — as, describedAs, withFailMessage, overridingErrorMessage, usingComparator, usingElementComparator, extracting, filteredOn — without ever reaching a real verification method afterward. A context method followed by any further call, whatever it is, is accepted as complete; only a chain that terminates in one of these is reported.

Rationale

AssertJ’s description and configuration methods only take effect for the assertion that comes after them in the chain — they attach a label, message or comparator to whatever verification follows. Placed at the very end of the chain instead, there is no following verification for them to configure, and no assertion ever ran: the statement builds and discards an object, and the test passes no matter what the code under test does.

assertThat(computeTotal()).isEqualTo(expectedTotal()).as("total mismatch");
// as() runs after isEqualTo() already returned -- nothing left for it to describe

Remediation

Move the context-setting call before the verification it is meant to describe.

assertThat(computeTotal()).as("total mismatch").isEqualTo(expectedTotal());

What is not reported

A chain whose result is handed on rather than discarded — assigned to a variable, returned, passed as an argument to another call, or produced as an expression lambda’s result — is left alone: the chain may be finished elsewhere, out of view.

© 2026 Xygeni. All rights reserved