OptionalSerializer implements StreamSerializer<Optional> { @Override public Optional read(ObjectDataInput in) throws IOException { Object result = in.readObject(); return result == null ? Optional.empty() : Optional.of(result); }  @Override void write(ObjectDataOutput out, Optional object); @Override Optional read(ObjectDataInput in); @Override int getTypeId(); @Override void destroy(); static SerializerConfig createConfig();  }
@Test public void readString() throws Exception { String test = "Test"; ObjectDataInput input = Mockito.mock(ObjectDataInput.class); Mockito.when(input.readObject()).thenReturn(test); Optional read = new OptionalSerializer().read(input); Assert.assertTrue(read.isPresent()); Assert.assertEquals(test, read.get()); }
@Test public void readEmpty() throws Exception { ObjectDataInput input = Mockito.mock(ObjectDataInput.class); Mockito.when(input.readObject()).thenReturn(null); Optional read = new OptionalSerializer().read(input); Assert.assertFalse(read.isPresent()); }