Spring RestTemplate & Spock 测试修改日期?
Spring RestTemplate & Spock Test modify Date?
我使用 Spock 来测试我的 api,它是这样的:
def "更新冲刺部分字段"() {
given: '冲刺DTO对象'
def startDate = new Date()
def endDate = MybatisFunctionTestUtil.dataSubFunction(startDate, 10)
SprintUpdateDTO sprintUpdateDTO = new SprintUpdateDTO()
sprintUpdateDTO.projectId = projectId
sprintUpdateDTO.sprintId = 2
sprintUpdateDTO.objectVersionNumber = 1
sprintUpdateDTO.endDate = endDate
sprintUpdateDTO.sprintName = '测试冲刺1'
sprintUpdateDTO.startDate = startDate
when: '分页过滤查询issue列表提供给测试模块用'
HttpEntity<SprintUpdateDTO> requestEntity = new HttpEntity<SprintUpdateDTO>(sprintUpdateDTO, null)
def entity = restTemplate.exchange('/v1/projects/{project_id}/sprint', HttpMethod.PUT, requestEntity, SprintDetailDTO, projectId)
then: '返回值'
entity.statusCode.is2xxSuccessful()
and: '设置值'
SprintDetailDTO sprintDetailDTO = entity.getBody()
expect: '设置期望值'
sprintDetailDTO.sprintId == 2
sprintDetailDTO.sprintName == '测试冲刺1'
sprintDetailDTO.projectId == projectId
sprintDetailDTO.startDate == startDate
sprintDetailDTO.endDate == endDate
}
我得到的结果是 startDate != startDate.
Condition not satisfied:
sprintDetailDTO.startDate == startDate
| | | |
| | | Mon Aug 20 10:51:56 CST 2018 (java.util.Date)
| | false
| Mon Aug 20 10:51:56 CST 2018 (java.util.Date)
io.choerodon.agile.api.dto.SprintDetailDTO@23488718
预计:
cdate: 2018-08-20T10:51:56.393+0800
defaultCenturyStart: 0
fastTime: 1534733516393
gcal: sun.util.calendar.Gregorian@6697a476
jcal: null
serialVersionUID: 7523967970034938905
ttb: [14, 1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 10000, 10000, 10000, 10300, 10240, 10360, 10300, 10420, 10360, 10480, 10420]
wtb: [am, pm, monday, tuesday, wednesday, thursday, friday, saturday, sunday, january, february, march, april, may, june, july, august, september, october, november, december, gmt, ut, utc, est, edt, cst, cdt, mst, mdt, pst, pdt]
实际:
cdate: null
defaultCenturyStart: 0
fastTime: 1534733516000
gcal: sun.util.calendar.Gregorian@6697a476
jcal: null
serialVersionUID: 7523967970034938905
ttb: [14, 1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 10000, 10000, 10000, 10300, 10240, 10360, 10300, 10420, 10360, 10480, 10420]
wtb: [am, pm, monday, tuesday, wednesday, thursday, friday, saturday, sunday, january, february, march, april, may, june, july, august, september, october, november, december, gmt, ut, utc, est, edt, cst, cdt, mst, mdt, pst, pdt]
我发现当我使用 'restTemplate.exchange' 控制器时,sprint 的开始日期和结束日期在 controller.I 中被修改想知道发生了什么?
在通过 REST 接口对日期进行序列化-反序列化时,您似乎失去了一些精度。
答:1534733516393
乙:1534733516000
一种补偿方法是舍弃毫秒:
sprintDetailDTO.startDategetTime()/1000 == startDate.getTime()/1000
我使用 Spock 来测试我的 api,它是这样的:
def "更新冲刺部分字段"() {
given: '冲刺DTO对象'
def startDate = new Date()
def endDate = MybatisFunctionTestUtil.dataSubFunction(startDate, 10)
SprintUpdateDTO sprintUpdateDTO = new SprintUpdateDTO()
sprintUpdateDTO.projectId = projectId
sprintUpdateDTO.sprintId = 2
sprintUpdateDTO.objectVersionNumber = 1
sprintUpdateDTO.endDate = endDate
sprintUpdateDTO.sprintName = '测试冲刺1'
sprintUpdateDTO.startDate = startDate
when: '分页过滤查询issue列表提供给测试模块用'
HttpEntity<SprintUpdateDTO> requestEntity = new HttpEntity<SprintUpdateDTO>(sprintUpdateDTO, null)
def entity = restTemplate.exchange('/v1/projects/{project_id}/sprint', HttpMethod.PUT, requestEntity, SprintDetailDTO, projectId)
then: '返回值'
entity.statusCode.is2xxSuccessful()
and: '设置值'
SprintDetailDTO sprintDetailDTO = entity.getBody()
expect: '设置期望值'
sprintDetailDTO.sprintId == 2
sprintDetailDTO.sprintName == '测试冲刺1'
sprintDetailDTO.projectId == projectId
sprintDetailDTO.startDate == startDate
sprintDetailDTO.endDate == endDate
}
我得到的结果是 startDate != startDate.
Condition not satisfied:
sprintDetailDTO.startDate == startDate
| | | |
| | | Mon Aug 20 10:51:56 CST 2018 (java.util.Date)
| | false
| Mon Aug 20 10:51:56 CST 2018 (java.util.Date)
io.choerodon.agile.api.dto.SprintDetailDTO@23488718
预计:
cdate: 2018-08-20T10:51:56.393+0800
defaultCenturyStart: 0
fastTime: 1534733516393
gcal: sun.util.calendar.Gregorian@6697a476
jcal: null
serialVersionUID: 7523967970034938905
ttb: [14, 1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 10000, 10000, 10000, 10300, 10240, 10360, 10300, 10420, 10360, 10480, 10420]
wtb: [am, pm, monday, tuesday, wednesday, thursday, friday, saturday, sunday, january, february, march, april, may, june, july, august, september, october, november, december, gmt, ut, utc, est, edt, cst, cdt, mst, mdt, pst, pdt]
实际:
cdate: null
defaultCenturyStart: 0
fastTime: 1534733516000
gcal: sun.util.calendar.Gregorian@6697a476
jcal: null
serialVersionUID: 7523967970034938905
ttb: [14, 1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 10000, 10000, 10000, 10300, 10240, 10360, 10300, 10420, 10360, 10480, 10420]
wtb: [am, pm, monday, tuesday, wednesday, thursday, friday, saturday, sunday, january, february, march, april, may, june, july, august, september, october, november, december, gmt, ut, utc, est, edt, cst, cdt, mst, mdt, pst, pdt]
我发现当我使用 'restTemplate.exchange' 控制器时,sprint 的开始日期和结束日期在 controller.I 中被修改想知道发生了什么?
在通过 REST 接口对日期进行序列化-反序列化时,您似乎失去了一些精度。
答:1534733516393
乙:1534733516000
一种补偿方法是舍弃毫秒:
sprintDetailDTO.startDategetTime()/1000 == startDate.getTime()/1000