Repository navigation
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 failureproxy(..) 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.
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();Proxies need two extra JVM options there; see Module Path.
assertj-validation · Apache License 2.0 · io.github.jinahya:assertj-validation · Java 17+ · Jakarta Validation 3.0 / 3.1 · AssertJ 3.27.7+
Start
Running validation
Inspecting results
Using it
Reference
Links