Confirm successful installation by checking the skill directory location:
.cursor/skills/unit-test-bean-validation
Restart Cursor to activate unit-test-bean-validation. Access via /unit-test-bean-validation in your agent's command palette.
โ
Security Notice
We perform automated surface-level scans (Gen AI Scanner, Socket, Snyk) during installation. These checks detect common vulnerabilities but do not guarantee complete security. Always review skill source code and verify the publisher's reputation before production use.
Skills execute code in your environment. Always review source, verify the publisher, and test in isolation before production.
This skill provides executable patterns for unit testing Jakarta Bean Validation annotations and custom validators using JUnit 5. Covers built-in constraints (@NotNull, @Email, @Min, @Max, @Size), custom @Constraint implementations, cross-field validation, and validation groups. Tests run in isolation without Spring context.
When to Use
Writing unit tests for Jakarta Bean Validation or JSR-380 constraints
Testing custom @Constraint validators and constraint violation messages
Testing bean validation logic in DTOs and request objects
classUserDtoTestextendsBaseValidationTest{@TestvoidshouldPassValidationWithValidUser(){UserDto user =newUserDto("Alice","[email protected]",25);assertThat(validator.validate(user)).isEmpty();}@TestvoidshouldFailWhenNameIsNull(){UserDto user =newUserDto(null,"[email protected]",25);assertThat(validator.validate(user)).extracting(ConstraintViolation::getMessage).contains("must not be blank");}@TestvoidshouldFailWhenEmailIsInvalid(){UserDto user =newUserDto("Alice","invalid-email",25);Set<ConstraintViolation<UserDto>> violations = validator.validate(user);assertThat(violations).extracting(ConstraintViolation::getPropertyPath).extracting(Path::toString).contains("email");}@TestvoidshouldFailWhenAgeIsBelowMinimum(){UserDto user =newUserDto("Alice","[email protected]",-1);assertThat(validator.validate(user)).extracting(ConstraintViolation::getMessage).contains("must be greater than or equal to 0");}@TestvoidshouldFailWhenMultipleConstraintsViolated(){UserDto user =newUserDto(null,"invalid",-5);assertThat(validator.validate(user)).hasSize(3);}}
Testing Custom Validators
For custom constraint patterns, see references/custom-validators.md:
Creating @Constraint annotations
Implementing ConstraintValidator
Cross-field validation (password matching)
Stateless validator best practices
Testing Validation Groups
For validation groups and parameterized tests, see references/advanced-patterns.md:
Defining validation group interfaces
Conditional validation with groups parameter
@ParameterizedTest with @ValueSource and @CsvSource
Debugging failed validation tests
Best Practices
Test both valid and invalid: Every constraint needs both passing and failing test cases
Assert violation details: Verify property path, message, and constraint type
Test edge cases: null, empty string, whitespace-only, boundary values
Keep validators stateless: Custom validators must not maintain state
Use clear messages: Constraint messages should be user-friendly
Group related tests: Extend BaseValidationTest to share validator setup
Test error messages: Ensure messages match requirements
Common Pitfalls
Forgetting to test null values (most constraints ignore null by default)
Not verifying the property path in constraint violations
Testing validation at service/controller level instead of unit level
Creating overly complex custom validators
Missing @NotNull for mandatory fields combined with other constraints
Constraints and Warnings
Null handling: Most constraints ignore null by default โ combine @NotNull with other constraints for mandatory fields
Thread safety: Validator instances are thread-safe and can be shared
Message localization: Test with different locales if i18n is required
Cascading validation: Use @Valid on nested objects for recursive validation
Custom validators: Must be stateless and return true for null values
Test isolation: Validation unit tests should not depend on Spring context or database
Troubleshooting
ValidatorFactory not found: Ensure jakarta.validation-api and hibernate-validator are on test classpath.
Custom validator not invoked: Verify @Constraint(validatedBy = YourValidator.class) annotation is correct.
Null values pass validation: This is expected behavior โ constraints ignore null unless @NotNull is present.
Wrong violation count: Use hasSize() to verify exact count, check all fields in the object.
Property path incorrect: Ensure the field, not the getter, has the constraint annotation.