Spring RestTemplate&Spock测试修改日期?

问题描述 投票:0回答:1

我使用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的startDate和endDate在控制器中被修改。我想知道发生了什么?

java spring-boot spock resttemplate
1个回答
0
投票

在通过REST接口序列化反序列化日期时,您似乎已经失去了一些精确度。

答:1534733516393 B:1534733516000

补偿的一种方法是丢弃毫秒:

sprintDetailDTO.startDategetTime()/1000 == startDate.getTime()/1000

© www.soinside.com 2019 - 2024. All rights reserved.