- Home
- Skills
- Giuseppe Trisciuoglio
- Developer Kit
- Unit Test Bean Validation
unit-test-bean-validation_skill
- Python
99
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill giuseppe-trisciuoglio/developer-kit --skill unit-test-bean-validation- SKILL.md15.0 KB
Overview
This skill provides concise, reusable patterns for unit testing Jakarta Bean Validation constraints and custom validators without loading a Spring context. It shows how to create a Validator, assert constraint violations, test cross-field and group-based validation, and cover edge cases using JUnit 5 and parameterized tests. Use it to verify data integrity and ensure validation rules behave as expected in isolation.
How this skill works
The skill demonstrates building a Validator via Validation.buildDefaultValidatorFactory().getValidator() and using validator.validate(...) to collect ConstraintViolation sets. It extracts property paths, messages, and invalid values for precise assertions. Examples include built-in annotations (@NotNull, @Email, @Min/@Max, @Size), custom ConstraintValidator implementations, cross-field constraints, validation groups, and parameterized test scenarios.
When to use it
- You need fast validation checks without booting Spring or the full application context
- Testing built-in annotations like @NotNull, @Email, @Min, @Max, @Size
- Verifying and unit-testing custom @Constraint validators and their error messages
- Testing cross-field rules such as password confirmation or interdependent fields
- Validating conditional rules using validation groups (create vs update)
Best practices
- Add jakarta.validation-api and hibernate-validator to the test classpath and use AssertJ for expressive assertions
- Create a Validator in @BeforeEach via Validation.buildDefaultValidatorFactory() for isolation or reuse a thread-safe instance if appropriate
- Write focused tests: one constraint per test and include both valid and invalid cases
- Assert violation details: property path, message text, and invalid value for precise feedback
- Use @ParameterizedTest to cover many input permutations and test edge cases (null, empty, whitespace)
Example use cases
- Unit-test DTO validation for incoming API payloads before wiring into controllers
- Verify a custom phone number validator rejects incorrect formats and accepts null when combined with @NotNull
- Test cross-field constraint ensuring newPassword equals confirmPassword on change-password objects
- Validate conditional rules with groups so required fields are enforced only on create operations
- Run parameterized email validation tests covering valid and invalid address formats
FAQ
No. The examples use Jakarta Validator directly so tests run fast without starting Spring or loading the application context.
Should custom validators accept null values?
Custom ConstraintValidator implementations should typically treat null as valid and rely on @NotNull when a value is required.