OptionalSerializer implements StreamSerializer<Optional> { @Override public void write(ObjectDataOutput out, Optional object) throws IOException { if (object.isPresent()) { out.writeObject(object.get()); } else { out.writeObject(null); } }  @Override void write(ObjectDataOutput out, Optional object); @Override Optional read(ObjectDataInput in); @Override int getTypeId(); @Override void destroy(); static SerializerConfig createConfig();  }
@Test public void writeString() throws Exception { String string = "String"; ObjectDataOutput output = Mockito.mock(ObjectDataOutput.class); new OptionalSerializer().write(output, Optional.of(string)); Mockito.verify(output, Mockito.times(1)).writeObject(string); }
@Test public void writeEmpty() throws Exception { ObjectDataOutput output = Mockito.mock(ObjectDataOutput.class); new OptionalSerializer().write(output, Optional.empty()); Mockito.verify(output, Mockito.times(1)).writeObject(null); }