无法验证集成测试用例中的日期类型 [英] Unable to validate date type in Integration test cases

查看:88
本文介绍了无法验证集成测试用例中的日期类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在编写Rest API的集成测试用例,其中我们将验证Response DTO的所有属性的类型。在DTO中,我们有一个类型为java.util.Date的属性。
但是,当尝试使用

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


预期:是java.util.Date
的实例,但:< 2343240000L>是java.lang.Long

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

请让我知道如何使用集成测试用例验证Date类型。

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

推荐答案

Spring默认使用@Jackson对JSON进行序列化,而@Jackson则默认将Jackson序列化为毫秒。如果您希望使用以下格式的日期,请尝试在DTO中的 testDate 字段中使用 @Temporal 批注进行批注:YYYY-MM-DD

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.

如果这两个解决方案都不起作用,则必须为日期字段定义一个自定义序列化程序以解决此问题,并用 @注释日期字段的get方法。 JsonSerialize(using = JsonDateSerializer.class)注释。

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将是您的自定义序列化器类。

JsonDateSerializer would be your custom serializer class.

有关自定义序列化程序的更多详细信息,请参见此答案
https://stackoverflow.com/a/38186623 / 6503002

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

这篇关于无法验证集成测试用例中的日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆