### Question:
TimestampDeserializer extends JsonDeserializer<Long> { @Override public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) { return p.getLongValue(); } if (p.hasToken(JsonToken.VALUE_STRING)) { try { return Instant.parse(p.getText()).toEpochMilli(); } catch (DateTimeParseException ignored) { return (Long) ctxt.handleWeirdStringValue(Long.class, p.getText(), "Cannot convert to valid Instant timestamp"); } } return (Long) ctxt.handleUnexpectedToken(Long.class, p); }  @Override Long deserialize(JsonParser p, DeserializationContext ctxt);  }### Answer:
@Test public void testDeserializeNumber() throws IOException { when(parser.hasToken(eq(JsonToken.VALUE_NUMBER_INT))).thenReturn(true); when(parser.getLongValue()).thenReturn(1480520820000L); assertEquals(1480520820000L, (long) deserializer.deserialize(parser, context)); }
@Test public void testDeserializeString() throws IOException { when(parser.hasToken(eq(JsonToken.VALUE_STRING))).thenReturn(true); when(parser.getText()).thenReturn("2016-11-30T15:47:00Z"); assertEquals(1480520820000L, (long) deserializer.deserialize(parser, context)); }
@Test(expected = InvalidFormatException.class) public void testDeserializeStringInvalidFormat() throws IOException { when(context.handleWeirdStringValue(eq(Long.class), eq("invalid"), anyString())).thenThrow(InvalidFormatException.class); when(parser.hasToken(eq(JsonToken.VALUE_STRING))).thenReturn(true); when(parser.getText()).thenReturn("invalid"); deserializer.deserialize(parser, context); }
@Test(expected = MismatchedInputException.class) public void testDeserializeInvalidToken() throws IOException { when(context.handleUnexpectedToken(eq(Long.class), eq(parser))).thenThrow(MismatchedInputException.class); deserializer.deserialize(parser, context); }