0

My REST service returns json like this:

{"lastWriteTime":1525507872.123000000 .... }

how do I match it in my mockMvc test using jsonPath statement?

when I try to use an is or equalTo matcher, like this:

andExpect(jsonPath("$.elements[0].lastWriteTime", equalTo(1525507872.123000000)))

I keep getting this kind of error in different flavours:

java.lang.AssertionError: JSON path "$.elements[0].lastWriteTime"
Expected: <1.525507872123E9>
     but: was <1525507872.123000000>

casting the value to double or specifying as string doesn't seem to work.

Greg
  • 1,227
  • 5
  • 23
  • 52
  • 1
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Iłya Bursov Jun 08 '21 at 21:13
  • 1
    Comparing floating-point values for equality is always error-prone due to limited precision. I would recommend to calculate the delta between the values and assert that the delta does not exceed a certain threshold. --- The "format" of this timestamp seems strange. Is there a specific reason for the floating point representation instead of a UNIX timestamp or an ISO 8601 string representation? – Turing85 Jun 08 '21 at 21:14
  • yeah I find it strange, too. this seems to be "default" serialization of OffsetDateTime with milliseconds. – Greg Jun 09 '21 at 07:09

0 Answers0