Search code examples
javaspring-bootspockresttemplate

Spring RestTemplate & Spock Test modify Date?


I use Spock to test my api,and it like this:

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
}

and I get result that 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

Expected:

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]

Actual:

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]

I find when I use 'restTemplate.exchange' to controller,sprint's startDate and endDate is modify in controller.I want to konw what happen?


Solution

  • It looks like you've lost some precision when serializing-deserializing the date over the REST interface.

    A: 1534733516393
    B: 1534733516000

    One way to compensate for that is to discard the milliseconds:

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