0

If I have a bean like

@Data
@Validated
@EqualsAndHashCode(callSuper = true)
@Document(collection = "Xxx")
public class XxxDocument extends Zzz {
    @Min(0)
    @Max(255)
    private Integer aProperty;
}

How can I write a test using assertJ that checks whether the setting of aProperty raised a ConstraintViolationException? I would like to use Lombok's @Data annotation to avoid writing the getters and setters where I could declare that the setter throws this exception.

du-it
  • 2,561
  • 8
  • 42
  • 80

2 Answers2

1

You could achieve that with AssertJ and some manual work, but assertj-bean-validation might be a better candidate for it.

Stefano Cordio
  • 1,687
  • 10
  • 20
0

As @StefanoCordio mentioned, with assertj-bean-validation, you can do this.

XxxDocument document = new XxxDocument(-1);

assertThatBean(document).isValid();                     // should fail
assertThatBean(document).hasValidProperty("aProperty"); // should fail

assertThatProperty(0).isValidFor(XxxDocument.class, "aProperty");
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184