### Question:
InstantSerializer extends StdSerializer<Instant> { @Override public void serialize(Instant instant, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException { try { if (instant == null) { gen.writeNull(); } else { gen.writeString(converter.convert(instant)); } } catch (Exception e) { throw new JsonGenerationException("Could not serialize instant: " + instant, e, gen); } } InstantSerializer(Converter<Instant, String> converter); @Override void serialize(Instant instant, JsonGenerator gen, SerializerProvider serializerProvider);  }### Answer:
@Test void should_serialize_instant_as_string() throws IOException { var expected = "20190101123456789"; var input = Instant.parse("2019-01-01T12:34:56.789Z"); given(converter.convert(input)).willReturn(expected); serializer.serialize(input, gen, provider); verify(converter).convert(input); verify(gen).writeString(expected); }
@Test void should_serialize_null_as_null() throws IOException { serializer.serialize(null, gen, provider); verify(converter, never()).convert(any()); verify(gen).writeNull(); }
@Test void should_throw_IOE_when_input_invalid() throws IOException { var error = new JsonGenerationException("not really", gen); var input = Instant.parse("2019-01-01T12:34:56.789Z"); var converted = "20190101123456789"; given(converter.convert(input)).willReturn(converted); willThrow(error).given(gen).writeString("20190101123456789"); assertThatThrownBy(() -> serializer.serialize(input, gen, provider)) .isInstanceOf(JsonGenerationException.class) .hasMessage("Could not serialize instant: " + input) .hasRootCause(error); }