0

We am writing Integration testcases for Rest API, where in we are validating the type of all the properties of Response DTO. In the DTO we have one property of type java.util.Date. However, when it try to use

 .andExpect(jsonPath("$.testDate", isA(Date.class)))

it fails our test case with

Expected: is an instance of java.util.Date but: <2343240000L> is a java.lang.Long

Please let me know How can we validate the Date type using integration test cases.

prasingh
  • 452
  • 4
  • 18
  • 1
    The date is serialised as millis which i think is default. you may want to disable property write timestamp as long in serialisation config assuming you're using Jackson. – s7vr Sep 28 '16 at 08:46

1 Answers1

1

Spring uses Jackson to serialize JSON and Jackson serialize dates as milliseconds by default as @Reddy says. Try annotate the testDate field in your DTO with @Temporal annotation if you want the date in this format: YYYY-MM-DD

If you want it in another format try with

@DateTimeFormat(pattern = "dd/MM/yyyy")

and change the pattern with the one you want.

If these two solutions do not work you have to define a custom serializer for date fields to solve the problem and annotate the get method of your date fields with the @JsonSerialize(using=JsonDateSerializer.class) annotation.

JsonDateSerializer would be your custom serializer class.

For more detail about the custom serializer see this answer https://stackoverflow.com/a/38186623/6503002

Community
  • 1
  • 1
amicoderozer
  • 2,046
  • 6
  • 28
  • 44