Skip to content

Failure Messages

Jin Kwon edited this page Nov 20, 2026 · 2 revisions

Failure Messages

Validating assertions

When a validating assertion fails, it says what was validated, with which groups, and lists every violation:

[registration case 42]
The set of constraint violations resulted while validating
	actual: User@3c1{name=null,age=0}
targeting
	groups: [jakarta.validation.groups.Default]
expected to be empty but contains 1 element(s)
	-> message        : must not be blank
	   propertyPath   : name
	   rootBeanClass  : class User
	   messageTemplate: {jakarta.validation.constraints.NotBlank.message}

The lines under validating depend on the method: actual and property for hasValidProperty; actual, beanType and property for isValidFor; actual and parameters of method / return value of method for method validation; parameters of constructor / return value of constructor for constructors. The negative methods say expected to be not empty but empty.

The failure goes through AssertJ, so the usual configuration applies:

  • as(..) / describedAs(..) prefixes it ([registration case 42] above);
  • withFailMessage(..) / overridingErrorMessage(..), eager or lazy, replaces it;
  • withRepresentation(..) formats the actual, property and group values.

A % in a value or a message is printed as is, not read as a format specifier.

Attribute checks name the attribute

A checking method on a violation or a descriptor, such as hasInvalidValue(..) or hasMessageTemplate(..), fails with the attribute name as its description, after yours if you set one:

[case 42: messageTemplate]
expected: "{jakarta.validation.constraints.NotNull.message}"
 but was: "{jakarta.validation.constraints.NotBlank.message}"

Navigation keeps your configuration

A navigated assertion — extractingViolations(), extractingPropertyPath(), nodes(), extracting...Node(int) — carries the description, overriding message and representation of the assertion it came from. Navigation within a path hands a lazy withFailMessage(Supplier) over lazily, so the supplier runs only if the navigated assertion fails.

Clone this wiki locally