Accepting DateFormat : 01042021
List item
Incorrect DateFormat : 9901042021 or 0104202199 etc.
My date validation code blocks:
@Value("${sms.date.format:ddMMyyyy}") private String DATE_FORMAT; public void validateDates(ReportRequest request){ DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); if(StringUtils.isNotBlank(request.getStartDate())){ try{ dateFormat.parse(request.getStartDate()); }catch (Exception e){ throw new InvalidInputException("Invalid start date format, must be as : " +DATE_FORMAT); } } else { throw new InvalidInputException("Contract start date is empty"); } if(StringUtils.isNotBlank(request.getEndDate())){ try{ dateFormat.parse(request.getEndDate()); }catch (Exception e){ throw new InvalidInputException("Invalid end date format, must be as : " +DATE_FORMAT); } } else { throw new InvalidInputException("Contract end date is empty"); } }
My Unit Test: Getting error on first line because dateformat is null how can i fix it?
@Value("${sms.date.format:ddMMyyyy}") private String DATE_FORMAT; public void testValidateDates() { DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); ReportRequest reportRequest = new ReportRequest(); ReportRequest.setStartDate("0104202199"); ReportRequestValidator reportRequestValidator = new ReportRequestValidator(); ReportRequestValidator.validateDates(reportRequest); assertEquals("Invalid start date format, must be as : ","0104202199"); }
How should I use the assert function, what should the condition be?