Close

AssertJ - Assertion Description with as()

[Last Updated: Mar 3, 2026]

When an assertion fails, the default error message tells you what was expected and what was received. But without context, it can be hard to understand which part of a complex test failed. AssertJ's as(String description, Object... args) method lets you attach a human-readable label to any assertion, which is prepended to the error message in square brackets.

When to Use as()

Use as() whenever the failing assertion may not be immediately clear from context — especially for boolean assertions, numeric comparisons in loops, or when asserting multiple fields of the same object in sequence. It acts as test documentation embedded directly in the assertion.

Important: Call as() Before the Assertion

The description must be set before the assertion method is called. If a failing assertion throws before reaching as(), the description is never applied. A common mistake is placing as() after the assertion:

// WRONG: as() is ignored if assertion fails first
assertThat(actual).isEqualTo(expected).as("my check");

// CORRECT:
assertThat(actual).as("my check").isEqualTo(expected);

Format String Support

as() accepts a printf-style format string and arguments, making it convenient for parameterized descriptions:

assertThat(user.getAge()).as("check %s's age", user.getName()).isGreaterThan(18);

Example

package com.logicbig.example;

import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;

public class AssertionDescriptionExample {

    record User(String name,
                int age) {}

    @Test
    void myTest() {
        User alice = new User("Alice", 30);

        // describe before the assertion
        assertThat(alice.name()).as("check user name").isEqualTo("Alice");
        System.out.println("Named assertion passed");

        // format string in description
        assertThat(alice.age()).as("check %s's age", alice.name())
                               .isGreaterThan(18);
        System.out.println("Formatted description passed");

        // multiple assertions with individual descriptions
        assertThat(alice.name()).as("name not blank").isNotBlank();
        assertThat(alice.age()).as("valid age range").isBetween(0, 120);
        System.out.println("All described assertions passed");
    }
}

Output

$ mvn clean test -Dtest=*
[INFO] Scanning for projects...
[INFO]
[INFO] ---------< com.logicbig.example:assertj-assertion-description >---------
[INFO] Building assertj-assertion-description 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ assertj-assertion-description ---
[INFO] Deleting D:\example-projects\assertj\assertj-assertion-description\target
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ assertj-assertion-description ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-assertion-description\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-assertion-description ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-assertion-description ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-assertion-description\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-assertion-description ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 1 source file with javac [debug target 17] to target\test-classes
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ assertj-assertion-description ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.logicbig.example.AssertionDescriptionExample
Named assertion passed
Formatted description passed
All described assertions passed
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.086 s -- in com.logicbig.example.AssertionDescriptionExample
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.471 s
[INFO] Finished at: 2026-03-03T13:38:36+08:00
[INFO] ------------------------------------------------------------------------

Conclusion

All assertions pass in the example, confirming the syntax is correct. In a real test failure, the description provided via as() would appear in brackets at the start of the error message, giving the developer immediate context about which field or condition failed without needing to trace through the test code manually.

Example Project

Dependencies and Technologies Used:

  • assertj-core 3.27.7 (Rich and fluent assertions for testing in Java)
  • junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
  • JDK 17
  • Maven 3.9.11

AssertJ - Assertion Description with as() Select All Download
  • assertj-assertion-description
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • AssertionDescriptionExample.java

    See Also

    Join