WrappingHttpOutputMessage implements HttpOutputMessage, AutoCloseable { @Nonnull @Override public OutputStream getBody() throws IOException { if (stream == null) { stream = wrapper.apply(message.getBody()); } return stream; }  @Nonnull @Override OutputStream getBody(); @Nonnull @Override HttpHeaders getHeaders(); @Override void close();  }
@Test void shouldReturnWrapped() throws IOException { final OutputStream wrappedStream = mock(OutputStream.class); when(wrapper.apply(any())).thenReturn(wrappedStream); assertThat(unit.getBody(), equalTo(wrappedStream)); assertThat(unit.getBody(), equalTo(wrappedStream)); }
@Test void shouldWrapOnFirstAccessOnly() throws IOException { final OutputStream wrappedStream = mock(OutputStream.class); when(wrapper.apply(any())).thenReturn(wrappedStream); unit.getBody(); unit.getBody(); verify(wrapper).apply(message.getBody()); verifyNoMoreInteractions(wrapper); }