Repository navigation
Executable Validation
Method and constructor validation go through
ExecutableValidator
(validator.forExecutables()), with the validator and groups configured on the assertion.
Method validation needs the instance the method belongs to, so it hangs off assertThatBean.
class Greeter {
@NotBlank
String greet(@NotBlank String name, @Positive int times) { ... }
}
Method greet = Greeter.class.getDeclaredMethod("greet", String.class, int.class);
assertThatBean(new Greeter())
.hasValidParameters(greet, "world", 1)
.doesNotHaveValidParameters(greet, "", 1)
.hasValidReturnValue(greet, "Hello, world")
.doesNotHaveValidReturnValue(greet, " ");| Method | Validates with |
|---|---|
hasValidParameters(Method, Object...) |
validateParameters(actual, method, values, groups), expecting none |
hasValidParameters(Method, Object[], Consumer<...>) |
same, handing the violations to the consumer first |
doesNotHaveValidParameters(Method, Object...) |
same, expecting some |
hasValidReturnValue(Method, Object) |
validateReturnValue(actual, method, value, groups), expecting none |
hasValidReturnValue(Method, Object, Consumer<...>) |
same, handing the violations to the consumer first |
doesNotHaveValidReturnValue(Method, Object) |
same, expecting some |
The method is not invoked: the values are passed in. A null method is a NullPointerException; a null bean is an
assertion failure.
There is no instance before construction, so constructor validation takes the Constructor as the actual value:
assertThatConstructor(Constructor<? extends T>) returns an AbstractConstructorAssert<?, T>.
class Account {
@ValidAccount // a class-level constraint, checked on the created object
Account(@NotBlank String owner, @PositiveOrZero long balance) { ... }
}
Constructor<Account> constructor = Account.class.getDeclaredConstructor(String.class, long.class);
assertThatConstructor(constructor)
.hasValidParameters("Jane", 0L)
.doesNotHaveValidParameters("", -1L)
.hasValidReturnValue(new Account("Jane", 0L));| Method | Validates with |
|---|---|
hasValidParameters(Object...) |
validateConstructorParameters(actual, values, groups), expecting none |
hasValidParameters(Object[], Consumer<...>) |
same, handing the violations to the consumer first |
doesNotHaveValidParameters(Object...) |
same, expecting some |
hasValidReturnValue(T) |
validateConstructorReturnValue(actual, created, groups), expecting none |
hasValidReturnValue(T, Consumer<...>) |
same, handing the violations to the consumer first |
doesNotHaveValidReturnValue(T) |
same, expecting some |
A null created object is the provider's IllegalArgumentException, not an assertion failure.
A parameter's path node is named by the provider's ParameterNameProvider. Hibernate Validator's default gives
arg0, arg1, ... unless the code was compiled with -parameters. Keep that in mind when asserting on paths:
assertThatBean(new Greeter()).doesNotHaveValidParameters(greet, "", 1);
Set<ConstraintViolation<Greeter>> violations = validator.forExecutables()
.validateParameters(new Greeter(), greet, new Object[]{"", 1});
assertThatConstraintViolations(violations)
.singleElement()
.extractingPropertyPath()
.hasSize(2)
.hasMethodNodeSatisfying(0, n -> n.hasName("greet").hasParameterTypes(String.class, int.class))
.hasParameterNodeSatisfying(1, n -> n.hasParameterIndex(0));See Paths and Nodes.
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