### Question:
GsonProvider implements MessageBodyReader<T>, MessageBodyWriter<T> { @Override public void writeTo( T object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream ) throws IOException, WebApplicationException { PrintWriter printWriter = new PrintWriter( entityStream ); try { String json = gson.toJson( object ); printWriter.write( json ); printWriter.flush(); } finally { printWriter.close(); } } GsonProvider(); @Override long getSize( T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType  ); @Override boolean isWriteable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ); @Override void writeTo( T object,
			Class<?> type,
			Type genericType,
			Annotation[] annotations,
			MediaType mediaType,
			MultivaluedMap<String, Object> httpHeaders,
			OutputStream entityStream ); @Override boolean isReadable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ); @Override T readFrom( Class<T> type,
			Type genericType,
			Annotation[] annotations,
			MediaType mediaType,
			MultivaluedMap<String, String> httpHeaders,
			InputStream entityStream );  }### Answer:
@Test public void testWritesTestObject() throws WebApplicationException, IOException { GsonProvider<TestObject> testObjectProvider = new GsonProvider<TestObject>(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); TestObject testObject = new TestObject( "foo" ); testObjectProvider.writeTo( testObject, TestObject.class, null, null, null, null, stream ); TestObject actualTestObject = new Gson().fromJson( convertToReader( stream ), TestObject.class ); assertThat( actualTestObject ).isEqualTo( testObject ); }
@Test public void testWritesTestObjectClosesStream() throws WebApplicationException, IOException { GsonProvider<TestObject> testObjectProvider = new GsonProvider<TestObject>(); OutputStream stream = mock( OutputStream.class ); TestObject testObject = new TestObject( "foo" ); testObjectProvider.writeTo( testObject, TestObject.class, null, null, null, null, stream ); verify( stream ).close(); }