### Question:
JacksonPrinter extends Printer { @Override protected String serialize(Object input) throws IllegalArgumentException { try { return objectMapper.writeValueAsString(input); } catch (JsonProcessingException e) { throw new IllegalArgumentException(e); } } JacksonPrinter(ObjectMapper objectMapper);   }### Answer:
@Test public void serialize() { ObjectMapper objectMapper = new ObjectMapper(); JacksonPrinter jacksonPrinter = new JacksonPrinter(objectMapper); Empty empty = new Empty(); empty.setValue("value"); String json = jacksonPrinter.serialize(empty); assertThat(json, is("{\"value\":\"value\"}")); }
@Test(expected = IllegalArgumentException.class) public void serializeException() throws JsonProcessingException { ObjectMapper objectMapper = mock(ObjectMapper.class); when(objectMapper.writeValueAsString(any())).thenThrow(new JsonGenerationException("", (JsonGenerator) null)); JacksonPrinter jacksonPrinter = new JacksonPrinter(objectMapper); String string = "string"; jacksonPrinter.serialize(string); }
@Test public void serializeXmlRoot() { JacksonPrinter jacksonPrinter = new JacksonPrinter(xmlMapper); Root input = new Root(); input.setValue("value"); String xml = jacksonPrinter.serialize(input); assertThat(xml, is("<someName><value>value</value></someName>")); }
@Test public void serializeXmlNotRoot() { JacksonPrinter jacksonPrinter = new JacksonPrinter(xmlMapper); NotRoot input = new NotRoot(); input.setValue("value"); String xml = jacksonPrinter.serialize(input); assertThat(xml, is("<NotRoot><value>value</value></NotRoot>")); }
@Test public void serializeXmlEmpty() { JacksonPrinter jacksonPrinter = new JacksonPrinter(xmlMapper); Empty input = new Empty(); input.setValue("value"); String xml = jacksonPrinter.serialize(input); assertThat(xml, is("<Empty><value>value</value></Empty>")); }