P singh P singh - 29 days ago 4
reST (reStructuredText) Question

Unable to validate date type in Integration test cases

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.


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 http://stackoverflow.com/a/38186623/6503002