### Question:
OptionalSerializerExtension implements SerializerExtension { @Override public Optional<Serializer> getSerializer( TypeMirror typeMirror, SerializerFactory factory, ProcessingEnvironment processingEnv) { if (!isOptional(typeMirror)) { return Optional.empty(); } TypeMirror containedType = getContainedType(typeMirror); Serializer containedTypeSerializer = factory.getSerializer(containedType); return Optional.of(new OptionalSerializer(containedTypeSerializer)); } OptionalSerializerExtension(); @Override Optional<Serializer> getSerializer(
      TypeMirror typeMirror, SerializerFactory factory, ProcessingEnvironment processingEnv);  }### Answer:
@Test public void toProxy() { TypeMirror typeMirror = declaredTypeOf(Optional.class, Integer.class); Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); CodeBlock actualCodeBlock = serializer.toProxy(CodeBlock.of("x")); assertThat(actualCodeBlock.toString()).isEqualTo("x.isPresent() ? x.get() : null"); }
@Test public void fromProxy() { TypeMirror typeMirror = declaredTypeOf(Optional.class, Integer.class); Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); CodeBlock actualCodeBlock = serializer.fromProxy(CodeBlock.of("x")); assertThat(actualCodeBlock.toString()) .isEqualTo("java.util.Optional.ofNullable(x == null ? null : x)"); }
@Test public void getSerializer_nonOptional_emptyReturned() { TypeMirror typeMirror = typeMirrorOf(String.class); Optional<Serializer> actualSerializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment); assertThat(actualSerializer).isEmpty(); }
@Test public void getSerializer_optional_serializerReturned() { TypeMirror typeMirror = typeMirrorOf(Optional.class); Serializer actualSerializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); assertThat(actualSerializer.getClass().getName()) .contains("OptionalSerializerExtension$OptionalSerializer"); }
@Test public void proxyFieldType() { TypeMirror typeMirror = declaredTypeOf(Optional.class, Integer.class); Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); TypeMirror actualTypeMirror = serializer.proxyFieldType(); assertThat(actualTypeMirror).isEqualTo(typeMirrorOf(Integer.class)); }