PatchSetSerializer implements Serializer<PatchSet> { @Override public byte[] serialize(String topic, PatchSet patch) { try { return objectWriter.writeValueAsBytes(patch); } catch (JsonProcessingException e) { throw new RuntimeException(e); } } PatchSetSerializer();  PatchSetSerializer(ObjectWriter objectWriter); @Override void configure(Map<String, ?> configs, boolean isKey); @Override byte[] serialize(String topic, PatchSet patch); @Override void close();  }
@Test public void deserialize() throws IOException { byte[] data = new byte[] { 0 }; when(objectWriter.writeValueAsBytes(patch)).thenReturn(data); byte[] result = underTest.serialize("topic", patch); assertThat(result, is(data)); }
@Test(expected = RuntimeException.class) public void deserializeThrowsException() throws Exception { doThrow(JsonProcessingException.class).when(objectWriter).writeValueAsBytes(patch); underTest.serialize("topic", patch); }
@Test public void defaultMapper() throws Exception { try (PatchSetSerializer serializer = new PatchSetSerializer()) { PatchSet patchSet = new PatchSet("road1", new ArrayList<>()); byte[] bytes = serializer.serialize("topic", patchSet); ObjectReader reader = new ObjectMapper().readerFor(PatchSet.class); assertThat(reader.readValue(bytes), is(patchSet)); } }