CustomDateSerializer extends StdSerializer<Date> { @Override public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException { if(value == null) return; this.serializer.serialize(LocalDateTime.ofInstant(value.toInstant(), ZoneOffset.UTC.normalized()), gen, provider); } CustomDateSerializer(); protected  CustomDateSerializer(Class<Date> t); @Override void serialize(Date value, JsonGenerator gen, SerializerProvider provider);  }
@Test public void t1_serialize() throws IOException { Date date = new GregorianCalendar(2018, Calendar.FEBRUARY, 11).getTime(); String expected = (new DateTime(date).withZone(DateTimeZone.UTC).toDateTimeISO().toString()); Mockito.doNothing().when(this.gen).writeString(Mockito.anyString()); this.testee.serialize(date, gen, null); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); Mockito.verify(this.gen).writeString(captor.capture()); Assert.assertEquals(expected, captor.getValue()); }
@Test public void t1_serializeBadValue() throws IOException { Date date = new GregorianCalendar(2018, Calendar.FEBRUARY, 11).getTime(); Mockito.doThrow(new IOException()).when(this.gen).writeString(Mockito.anyString()); this.thrown.expect(JsonParsingException.class); this.testee.serialize(date, gen, null); }