Repository navigation
InstanceOf Factories
ValidationInstanceOfAssertFactories holds AssertJ InstanceOfAssertFactory values for the Jakarta Validation types,
for asInstanceOf(…) and for the factory-taking navigation methods of AssertJ (singleElement(…), first(…),
element(int, …), extracting(…, factory)) and of this library.
As in AssertJ's own InstanceOfAssertFactories, the non-generic types are constants and the generic ones are
methods.
| Factory | For | Produces |
|---|---|---|
constraintViolation() |
ConstraintViolation<T> |
AbstractConstraintViolationAssert<?, ConstraintViolation<T>, T> |
constraintDescriptor() |
ConstraintDescriptor<T> |
AbstractConstraintDescriptorAssert<?, ?, T> |
| Group | Constants |
|---|---|
| exception |
CONSTRAINT_VIOLATION_EXCEPTION (not in 0.6.1)
|
| path | PATH |
| nodes |
NODE, BEAN_NODE, PROPERTY_NODE, METHOD_NODE, CONSTRUCTOR_NODE, PARAMETER_NODE, CROSS_PARAMETER_NODE, RETURN_VALUE_NODE, CONTAINER_ELEMENT_NODE
|
| metadata |
BEAN_DESCRIPTOR, PROPERTY_DESCRIPTOR, METHOD_DESCRIPTOR, CONSTRUCTOR_DESCRIPTOR, PARAMETER_DESCRIPTOR, RETURN_VALUE_DESCRIPTOR, CROSS_PARAMETER_DESCRIPTOR, CONTAINER_ELEMENT_TYPE_DESCRIPTOR, GROUP_CONVERSION_DESCRIPTOR
|
Twenty constants in all; a test guards against one being dropped. Each produces the assertion the corresponding
ValidationAssertions.assertThat… entry point returns.
import static com.github.jinahya.assertj.validation.ValidationInstanceOfAssertFactories.*;
// an Object that is known to be a Path
assertThat((Object) violation.getPropertyPath())
.asInstanceOf(PATH)
.hasSize(2);
// a node, narrowed to its kind
assertThat((Object) node)
.asInstanceOf(METHOD_NODE)
.hasName("greet")
.extractingParameterTypes().containsExactly(String.class, int.class);
// an element of a plain CollectionAssert
assertThatConstraintViolationException(exception)
.extractingConstraintViolations()
.singleElement()
.asInstanceOf(constraintViolation())
.hasMessageTemplate("{jakarta.validation.constraints.NotBlank.message}");
assertThatBeanDescriptor(descriptor)
.extractingConstraintsForProperty("tags")
.extractingConstrainedContainerElementTypes()
.singleElement(CONTAINER_ELEMENT_TYPE_DESCRIPTOR)
.hasContainerClass(List.class);
// an exception reached as a cause
assertThat(thrown)
.cause()
.asInstanceOf(CONSTRAINT_VIOLATION_EXCEPTION)
.hasViolationOn("name");asInstanceOf first performs an instanceof test, which cannot tell node kinds apart: Hibernate Validator's node
implements every Path.Node subtype at once. So each typed node factory — BEAN_NODE through
CONTAINER_ELEMENT_NODE — additionally checks Path.Node.getKind() and fails with an AssertionError naming the
expected and actual kinds. Without it, asInstanceOf(PROPERTY_NODE) on a method node would succeed, and fail later
from inside the provider. NODE accepts any node. See
Kind checking.
There is no factory for the assertions which run validation — the bean, property-value and constructor assertions
— and none for the set of violations. Those are where a test starts rather than values it meets while navigating, and
a factory would have to fix their root bean type. Use the ValidationAssertions entry points for them.
assertj-validation · Apache License 2.0 · io.github.jinahya:assertj-validation · Jakarta Validation 3.0 / 3.1 · AssertJ 3.27+ · Java 17+
Running validation
Inspecting results
Reference
External