Skip to content

Validating Values

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

Validating Values

assertThatProperty(actual) returns an AbstractPropertyAssert<?, ACTUAL>. It checks a value against the constraints declared on a property of a bean type, without an instance of that type, through Validator.validateValue(beanType, propertyName, actual, groups).

Method Passes when
<T> isValidFor(Class<T> beanType, String propertyName) the set of violations is empty
<T> isValidFor(Class<T> beanType, String propertyName, Consumer<? super Set<ConstraintViolation<T>>> consumer) the set is empty
<T> isNotValidFor(Class<T> beanType, String propertyName) the set is not empty

actual may be null — it is a value like any other, and @NotNull / @NotBlank decide about it. A null beanType, propertyName or consumer is a NullPointerException. A property name the bean type does not have is the provider's to reject; Jakarta Validation specifies an IllegalArgumentException for it.

assertThatProperty("Jane").isValidFor(User.class, "name");    // passes
assertThatProperty(null).isValidFor(User.class, "name");      // fails
assertThatProperty("").isValidFor(User.class, "name");        // fails
assertThatProperty(" ").isValidFor(User.class, "name");       // fails

assertThatProperty(0).isValidFor(User.class, "age");          // passes
assertThatProperty(-1).isValidFor(User.class, "age");         // fails
assertThatProperty(300).isValidFor(User.class, "age");        // fails

Groups are configured with targetingGroups, as on Validating Beans:

assertThatProperty(60)
        .isValidFor(User.class, "age")
        .targetingGroups(Junior.class).isNotValidFor(User.class, "age")
        .targetingGroups(Senior.class).isValidFor(User.class, "age");

The consumer of isValidFor(…, Consumer) runs before the emptiness check and receives an unmodifiable view, so it sees the violations even when the assertion then fails. That differs from other overloads and is open in #9; isNotValidFor has no consumer overload (#12). See Consumers.

Checking a bean as a property value

assertThatBean returns a subclass of the property assertion, so a bean can be checked against a property of another type too. The test fixtures have a registration whose user is constrained differently per subclass:

abstract class Registration {
    @Valid @NotNull final User user;
    …
}

class JuniorRegistration extends Registration {
    @Junior                         // a constraint: the user's age is at most 59
    @Override User getUser() { return super.getUser(); }
}

class SeniorRegistration extends Registration {
    @Senior                         // a constraint: the user's age is at least 60
    @Override User getUser() { return super.getUser(); }
}
assertThatBean(new User("Jane", 28))
        .isValid()
        .isValidFor(JuniorRegistration.class, "user")
        .isNotValidFor(SeniorRegistration.class, "user");

(In the repository's fixtures Junior and Senior are constraint annotations, validated by JuniorValidator and SeniorValidator, and are also used as the group interfaces on User.age.)

The @Valid caveat

validateValue does not honour @Valid. Checking a value against Registration.user evaluates the constraints declared on the user property — @NotNull, and the @Junior / @Senior constraint of a subclass — but does not cascade into the User to check its own name and age. So a User whose own constraints are violated can still pass isValidFor(Registration.class, "user").

When the referenced object's own constraints matter, assert on it directly with assertThatBean(user).isValid(), or validate an instance of the owning bean.

Clone this wiki locally