英文:
Jackson.InvalidDefinitionException: Cannot construct instance of java.time.OffsetDateTime in JUnit @BeforeEach
问题
我正试图为我的Open-API自动生成的代码创建单元测试用例。我有一个date-time
格式,在API中运行正常,但在设置我的JUnit测试用例时失败。
JUnit设置代码:
@BeforeEach
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
requestDTO = objectMapper.readValue(TestData.TEST_JSON, RequestDTO.class);
}
TestData.TEST_JSON
:
public static String TEST_JSON = "{
\"name\": \"credit_application\",
\"comments\": {
\"id\": \"1\",
\"userId\": \"21212\",
\"taskId\": \"212\",
\"time\": \"2010-07-17T02:49:58.564Z\",
\"comment\": \"Hello world\"
},
\"id\": \"123\",
\"version\": \"1.0\"
}";
RequestDTO
(Open-API自动生成的代码):
public class RequestDTO {
@JsonProperty("name")
private String name;
@JsonProperty("comments")
private CommentDTO comments;
@JsonProperty("id")
private String id;
// 构造函数和Getter/Setter方法
}
CommentDTO
(Open-API自动生成的代码):
public class CommentDTO {
@JsonProperty("id")
private String id;
@JsonProperty("userId")
private String userId;
@JsonProperty("taskId")
private String taskId;
@JsonProperty("time")
private OffsetDateTime time;
@JsonProperty("comment")
private String comment;
// 构造函数和Getter/Setter方法
}
异常:
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')
这个`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:
@BeforeEach
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
requestDTO = objectMapper.readValue(TestData.TEST_JSON, RequestDTO.class);
}
TestData.TEST_JSON
:
public static String TEST_JSON= "{\n" +
" \"name\": \"credit_application\",\n" +
" \"comments\": {\n" +
" \"id\": \"1\",\n" +
" \"userId\": \"21212\",\n" +
" \"taskId\": \"212\",\n" +
" \"time\": \"2010-07-17T02:49:58.564Z\",\n" +
" \"comment\": \"Hello world\"\n" +
" },\n" +
" \"id\": \"123\",\n" +
" \"version\": \"1.0\"\n" +
"}";
/*
{
"name": "credit_application",
"comments": {
"id": "1",
"userId": "21212",
"taskId": "212",
"time": "2010-07-17T02:49:58.564Z",
"comment": "Hello world"
},
"id": "123",
"version": "1.0"
}
*/
RequestDTO
(Open-API autogenerated code):
public class RequestDTO {
@JsonProperty("name")
private String name;
@JsonProperty("comments")
private CommentDTO comments;
@JsonProperty("id")
private String id;
// Constructors and Getters/Setters
}
CommentDTO
(Open-API autogenerated code):
public class CommentDTO {
@JsonProperty("id")
private String id;
@JsonProperty("userId")
private String userId;
@JsonProperty("taskId")
private String taskId;
@JsonProperty("time")
private OffsetDateTime time;
@JsonProperty("comment")
private String comment;
// Constructors and Getters/Setters
}
Exception:
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,使用如下:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.10</version>
</dependency>
然后将该模块注册到你的ObjectMapper中:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.readValue(TestData.TEST_JSON, RequestDTO.class);
英文:
You need to use JavaTimeModule to configure the ObjectMapper to deal with Java DateTime API
using :
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.10</version>
</dependency>
Then register the module to you ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.readValue(TestData.TEST_JSON, RequestDTO.class);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论