2
    Accepting DateFormat : 01042021
  1. 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?

stromboli
  • 41
  • 1
  • 5
  • Does this answer your question? [Populating Spring @Value during Unit Test](https://stackoverflow.com/questions/17353327/populating-spring-value-during-unit-test) – hous Apr 01 '22 at 00:18

1 Answers1

1

The problem is that DATE_FORMAT is not injected into your test class, so you get a NullPointerException.

Two way to solve it

  1. use @SpringBootTest. Check this: https://www.baeldung.com/spring-tests-override-properties

  2. a more clean way: without any Spring annotation. This makes the test code much simpler. Just refactor your code:

// in your production code:
public class ReportRequestValidator {

  private String DATE_FORMAT;

  // package-private constructor for test 
  ReportRequestValidator (@Value("${sms.date.format:ddMMyyyy}") String dateFormat) {
     DATE_FORMAT = dateFormat;
  }

  // business logic...
 
}


// in your test code

@Rule
public ExpectedException expectedEx;

@Before
public void init() {
  expectedEx = ExpectedException.none();
}

@Test
public void testValidateDates() {
    String DATE_FORMAT = "PROVIDE_DATE_FORMAT_STRING_BY_YOURSELF";
    ReportRequest reportRequest = new ReportRequest();
    ReportRequest.setStartDate("0104202199");
    ReportRequestValidator reportRequestValidator = new ReportRequestValidator(DATE_FORMAT);    // inject DATE_FORMAT into validator

    ReportRequestValidator.validateDates(reportRequest);

    expectedEx.expect(InvalidInputException.class);
    expectedEx.expectMessage("Invalid start date format, must be as : 0104202199");
}
Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21