I have the cDate
in my model class below, and want to write a test to validate the format is as specified. The test is below (I have omitted some for brevity), but it fails with an exception (below). Originally this was a String
type which was fine as I can use a @Pattern(regexp = "")
but the requirement is to use LocalDate
. When I use the correct format in the test, it correctly passes as it is being compared to the @JsonFormat
pattern. So how can I validate the format of LocalDate
type? I have also tried adding throws DateTimeParseException
to the test and tried using / in the pattern.
@NotNull(message = "cDate is mandatory")
@JsonFormat(pattern = "yyyy-MM-dd")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate cDate;
@Test
public void shouldValidateDateFormatFailure() {
// Given
String test = LocalDate.now().format(DateTimeFormatter.ofPattern("yy-MM-dd"));
LocalDate date = LocalDate.parse(test);
....
java.time.format.DateTimeParseException: Text '20-06-04' could not be parsed at index 0