RequestBodyBuilderImpl implements RequestBodyBuilder { @Override public void json(Object payload) { try { string(mapper.writeValueAsString(payload), "application/json"); } catch (JsonProcessingException e) { throw new IllegalStateException(e); } } RequestBodyBuilderImpl(ObjectMapper mapper); @Override void string(String str, String mediaType); @Override void file(Path path, String mediaType); @Override void json(Object payload); RequestBody getBody(); String getFileName();  }
@Test public void testHandlingOfJsonProcessingException() throws JsonProcessingException { Object data = new Object(); JsonProcessingException exception = mock(JsonProcessingException.class); when(mapper.writeValueAsString(data)).thenThrow(exception); try { bodyBuilder.json(data); fail("No exception thrown!"); } catch (IllegalStateException iex) { assertThat(iex.getCause()).isEqualTo(exception); } }