### Question:
ImmutableListSerializerExtension implements SerializerExtension { @Override public Optional<Serializer> getSerializer( TypeMirror typeMirror, SerializerFactory factory, ProcessingEnvironment processingEnv) { if (!isImmutableList(typeMirror)) { return Optional.empty(); } TypeMirror containedType = getContainedType(typeMirror); Serializer containedTypeSerializer = factory.getSerializer(containedType); if (containedTypeSerializer.isIdentity()) { return Optional.empty(); } return Optional.of(new ImmutableListSerializer(containedTypeSerializer, processingEnv)); } ImmutableListSerializerExtension(); @Override Optional<Serializer> getSerializer(
      TypeMirror typeMirror, SerializerFactory factory, ProcessingEnvironment processingEnv);  }### Answer:
@Test public void getSerializer_nonImmutableList_emptyReturned() { TypeMirror typeMirror = typeMirrorOf(String.class); Optional<Serializer> actualSerializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment); assertThat(actualSerializer).isEmpty(); }
@Test public void getSerializer_immutableListWithSerializableContainedType_emptyReturned() { fakeSerializerFactory.setReturnIdentitySerializer(true); TypeMirror typeMirror = typeMirrorOf(ImmutableList.class); Optional<Serializer> actualSerializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment); assertThat(actualSerializer).isEmpty(); }
@Test public void getSerializer_immutableList_serializerReturned() { TypeMirror typeMirror = typeMirrorOf(ImmutableList.class); Serializer actualSerializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); assertThat(actualSerializer.getClass().getName()) .contains("ImmutableListSerializerExtension$ImmutableListSerializer"); }
@Test public void proxyFieldType() { TypeMirror typeMirror = declaredTypeOf(ImmutableList.class, Integer.class); Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); TypeMirror actualTypeMirror = serializer.proxyFieldType(); assertThat(typeUtils.isSameType(actualTypeMirror, typeMirror)).isTrue(); }
@Test public void toProxy() { TypeMirror typeMirror = declaredTypeOf(ImmutableList.class, Integer.class); Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); CodeBlock actualCodeBlock = serializer.toProxy(CodeBlock.of("x")); assertThat(actualCodeBlock.toString()) .isEqualTo( String.format( "x.stream().map(%s.wrapper(value$ -> value$)).collect(%s.toImmutableList())", FUNCTION_WITH_EXCEPTIONS, IMMUTABLE_LIST)); }
@Test public void fromProxy() { TypeMirror typeMirror = declaredTypeOf(ImmutableList.class, Integer.class); Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); CodeBlock actualCodeBlock = serializer.fromProxy(CodeBlock.of("x")); assertThat(actualCodeBlock.toString()) .isEqualTo( String.format( "x.stream().map(%s.wrapper(value$ -> value$)).collect(%s.toImmutableList())", FUNCTION_WITH_EXCEPTIONS, IMMUTABLE_LIST)); }