Jackson.InvalidDefinitionException: Cannot construct instance of java.time.OffsetDateTime in JUnit @BeforeEach

huangapple go评论91阅读模式
英文:

Jackson.InvalidDefinitionException: Cannot construct instance of java.time.OffsetDateTime in JUnit @BeforeEach

问题

我正试图为我的Open-API自动生成的代码创建单元测试用例。我有一个date-time格式,在API中运行正常,但在设置我的JUnit测试用例时失败。

JUnit设置代码:

  1. @BeforeEach
  2. public void setUp() throws IOException {
  3. MockitoAnnotations.initMocks(this);
  4. requestDTO = objectMapper.readValue(TestData.TEST_JSON, RequestDTO.class);
  5. }

TestData.TEST_JSON

  1. public static String TEST_JSON = "{
  2. \"name\": \"credit_application\",
  3. \"comments\": {
  4. \"id\": \"1\",
  5. \"userId\": \"21212\",
  6. \"taskId\": \"212\",
  7. \"time\": \"2010-07-17T02:49:58.564Z\",
  8. \"comment\": \"Hello world\"
  9. },
  10. \"id\": \"123\",
  11. \"version\": \"1.0\"
  12. }";

RequestDTO(Open-API自动生成的代码):

  1. public class RequestDTO {
  2. @JsonProperty("name")
  3. private String name;
  4. @JsonProperty("comments")
  5. private CommentDTO comments;
  6. @JsonProperty("id")
  7. private String id;
  8. // 构造函数和Getter/Setter方法
  9. }

CommentDTO(Open-API自动生成的代码):

  1. public class CommentDTO {
  2. @JsonProperty("id")
  3. private String id;
  4. @JsonProperty("userId")
  5. private String userId;
  6. @JsonProperty("taskId")
  7. private String taskId;
  8. @JsonProperty("time")
  9. private OffsetDateTime time;
  10. @JsonProperty("comment")
  11. private String comment;
  12. // 构造函数和Getter/Setter方法
  13. }

异常:

  1. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.OffsetDateTime`
  2. (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-07-27T02:49:58.564Z')
  3. 这个`date-time`在应用程序代码中可以使用并正常工作但在JUnit设置代码中不行
英文:

I am trying to create unit test cases for my Open-API auto-generated code. I have a date-time format which works fine in API but fails when I try to setup my JUnit test case

JUnit setup code:

  1. @BeforeEach
  2. public void setUp() throws IOException {
  3. MockitoAnnotations.initMocks(this);
  4. requestDTO = objectMapper.readValue(TestData.TEST_JSON, RequestDTO.class);
  5. }

TestData.TEST_JSON :

  1. public static String TEST_JSON= "{\n" +
  2. " \"name\": \"credit_application\",\n" +
  3. " \"comments\": {\n" +
  4. " \"id\": \"1\",\n" +
  5. " \"userId\": \"21212\",\n" +
  6. " \"taskId\": \"212\",\n" +
  7. " \"time\": \"2010-07-17T02:49:58.564Z\",\n" +
  8. " \"comment\": \"Hello world\"\n" +
  9. " },\n" +
  10. " \"id\": \"123\",\n" +
  11. " \"version\": \"1.0\"\n" +
  12. "}";
  13. /*
  14. {
  15. "name": "credit_application",
  16. "comments": {
  17. "id": "1",
  18. "userId": "21212",
  19. "taskId": "212",
  20. "time": "2010-07-17T02:49:58.564Z",
  21. "comment": "Hello world"
  22. },
  23. "id": "123",
  24. "version": "1.0"
  25. }
  26. */

RequestDTO (Open-API autogenerated code):

  1. public class RequestDTO {
  2. @JsonProperty("name")
  3. private String name;
  4. @JsonProperty("comments")
  5. private CommentDTO comments;
  6. @JsonProperty("id")
  7. private String id;
  8. // Constructors and Getters/Setters
  9. }

CommentDTO (Open-API autogenerated code):

  1. public class CommentDTO {
  2. @JsonProperty("id")
  3. private String id;
  4. @JsonProperty("userId")
  5. private String userId;
  6. @JsonProperty("taskId")
  7. private String taskId;
  8. @JsonProperty("time")
  9. private OffsetDateTime time;
  10. @JsonProperty("comment")
  11. private String comment;
  12. // Constructors and Getters/Setters
  13. }

Exception:

  1. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.OffsetDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-07-27T02:49:58.564Z')

This date-time is used and works for application code, but not in Junit setup code.

答案1

得分: 3

你需要使用JavaTimeModule来配置ObjectMapper以处理Java DateTime API,使用如下:

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.datatype</groupId>
  3. <artifactId>jackson-datatype-jsr310</artifactId>
  4. <version>2.9.10</version>
  5. </dependency>

然后将该模块注册到你的ObjectMapper中:

  1. ObjectMapper objectMapper = new ObjectMapper();
  2. objectMapper.registerModule(new JavaTimeModule());
  3. objectMapper.readValue(TestData.TEST_JSON, RequestDTO.class);
英文:

You need to use JavaTimeModule to configure the ObjectMapper to deal with Java DateTime API
using :

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
  3. &lt;artifactId&gt;jackson-datatype-jsr310&lt;/artifactId&gt;
  4. &lt;version&gt;2.9.10&lt;/version&gt;
  5. &lt;/dependency&gt;

Then register the module to you ObjectMapper

  1. ObjectMapper objectMapper = new ObjectMapper();
  2. objectMapper.registerModule(new JavaTimeModule());
  3. objectMapper.readValue(TestData.TEST_JSON, RequestDTO.class);

huangapple
  • 本文由 发表于 2020年7月27日 13:55:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63109404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定