Annotation processor - Unit testing inquiry

Hi!

I have an annotation processor library that I am very keen to get working for Kotlin, however I have hit a snag in terms of my unit testing. I would really appreciate if someone could give me some advice.

My current unit testing implementation uses Google’s compile-testing library. I create input and output classes and store them in the resources directory. Then during the unit test, the compile-testing library compiles the input java class, executes the annotation processor and then compares the generated classes against the output class from the resources directory.

Here is an example (from my project) of what I am referring to:
Unit test class
Resources (Input and output classes)

This is working great for all my current java based unit tests. However I’m hoping to write some unit tests that use Kotlin classes.

Unfortunately this does not appear to be that easy, as the compile-testing library is first and foremost a Java specific library (I don’t see any mention of Kotlin on their project)

At the moment I get the following issue:

java.lang.IllegalArgumentException: Compilation unit is not of SOURCE kind: “/C:/dev/gsonpath/gsonpath-compiler/build/resources/test/adapter/auto/field_types/primitives/valid/TestValidPrimitives.kt”

at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:137)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:107)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:64)
at com.google.testing.compile.Compilation.compile(Compilation.java:69)
at com.google.testing.compile.JavaSourcesSubject$CompilationClause.compilesWithoutError(JavaSourcesSubject.java:281)

The problem is fairly obvious that the incorrect compiler is being used.

Has anyone come across this issue before and solved it? I have had a look at a few other annotation processors (such as DBFlow), and they do not write unit tests in this manner.

1 Like

I’m not sure what kind of solution you expect. The README of compile-testing says explicitly: “A library for testing javac compilation”. So it has no support for compiling Kotlin files, and I don’t think it would be reasonable to expect that a contribution adding Kotlin support would be accepted. You’d need to use a different approach for writing your tests.

Thanks for the reply!

I thought perhaps since Kotlin now has annotation processing support that there might be a way to test similar to the solution above. I wasn’t suggesting that the compile-testing library should add Kotlin support, I only mentioned it so that I could explain my current unit testing implementation.

Do you have any recommendations for annotation processor unit tests that specifically test Kotlin classes? My main aim is to add support for Data Class.