Skip to content

Soft Assertions

Jin Kwon edited this page Nov 17, 2026 · 1 revision

Soft Assertions

Every assertion method is non-final, so AssertJ's SoftAssertions.proxy(..) can intercept it and collect a failure instead of throwing:

SoftAssertions softly = new SoftAssertions();
softly.proxy(BeanAssert.class, Object.class, user).isValid();
softly.proxy(BeanAssert.class, Object.class, user).hasValidProperty("age");
softly.proxy(ConstraintViolationAssert.class, ConstraintViolation.class, violation)
      .hasMessageTemplate("{jakarta.validation.constraints.NotBlank.message}");
softly.assertAll();   // reports every failure

proxy(..) takes the concrete class (BeanAssert, PropertyAssert, ConstraintViolationAssert, PathAssert, ...), the type of the actual value, and the value. JUnit 5's SoftAssertionsExtension works the same way.

Navigation is not soft

On AssertJ 3.x and 4.0.0-M1, AssertJ re-proxies only the results of its own navigation methods. A chain that navigates through this library's — extractingViolations(), extractingPropertyPath(), extractingNode(0), extractingConstraintDescriptor(), ... — leaves the soft proxy, and its first failure throws.

Wrap such a chain in check(..), which collects that first failure and moves on:

softly.check(() -> assertThatConstraintViolation(violation)
        .extractingPropertyPath()
        .hasSize(1)
        .extractingPropertyNode(0)
        .hasName("name"));
softly.check(() -> assertThatBean(user).extractingViolations().isEmpty());
softly.assertAll();

On the module path

Proxies need two extra JVM options there; see Module Path.

Clone this wiki locally