Search code examples
androidgradleannotation-processing

Test apt argument for annotation processor with google compile-testing


I am writing annotation processor for my android project and I test it using google compile-testing.

It all works fine except that I am able to test apt plugnin argument to my annotation processor.

My annotation processor has this option that I want to test:

@Override
public Set<String> getSupportedOptions() {
    Set<String> options = new HashSet<>();
    options.add("generated_class_suffix");
    return options;
}

I do not seem to get how I can pass this options to compile-testing library to test it. I tried withCompilerOptions as follows:

    assertAbout(javaSource())
            .that(source)
            .withCompilerOptions("saver_suffix")
            .processedWith(new CodegenProcessor())
            .compilesWithoutError()
            .and()
            .generatesSources(generated);

but it gives me following error:

java.lang.IllegalArgumentException: invalid flag: saver_suffix

I am not sure how to pass the option.


Solution

  • I found from the standard javac documentation how to pass options to annotation processor:

    -Akey[=value] 
    

    for example if I want to pass saver_suffix as Saver I need to pass -Asaver_suffix=Saver

    so I did:

     assertAbout(javaSource())
            .that(source)
            .withCompilerOptions("-Asaver_suffix=Saver")
            .processedWith(new CodegenProcessor())
            .compilesWithoutError()
            .and()
            .generatesSources(generated);