'Java Validation not working with @Value.Immutables
I have been at it for 2 days but I can't figure out what I am missing.
My Immutables class looks like:
import org.immutables.value.Value;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
@Value.Immutable
public abstract class _MyClass {
@Valid
@NotBlank
public abstract String field();
@Valid
@NotEmpty
public abstract Set<@Valid @NotBlank String> terms();
}
The libraries in my build file:
Javax Validation Api
Hibernate Validator
Problems:
- My generated class file for
MyClassdoes not contain the javax validation - The following test code fails:
public void testMyClassValidation() {
MyClass myClass = MyClass.builder().field("field").addAllTerms(Set.of()).build();
Set<ConstraintViolation<RankingHint>> constraintViolations = Validation.buildDefaultValidatorFactory().getValidator().validate(myClass);
assertEquals(1, constraintViolations.size()); // THIS IS ZERO
//assertEquals("may not be null", constraintViolations.iterator().next().getMessage());
}
What am I doing wrong? I found a discussion thread where Immutables are supposed to work with Javax validation: https://github.com/immutables/immutables/issues/26
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
