Now we can write some tests. These will be java unit tests, so we’ll put them inside the src/test/java/ folder.

We will either check generated java files

or error messages that should be thrown by the annotation processor.

INPUT_FILES and OUTPUT_FILES are lists of JavaFileObjects. ANNOTATION_PROCESSOR is an instance of the processor you are testing (you can optionally add more processors — e.g. I once needed to test if files generated by dagger2 are correctly processed by my processor). Lastly the ERROR_MESSAGE is a String that should be a part of the error message.

A simple test of valid @Logged usage could look like this:

LoggedTest.java

And a test of invalid @Logged usage could look something like this:

LoggedTest.java

As you can see, we created java files using JavaFileObjects.forSourceString(fullName, source) and then called the assertion. There’s not much to explain, but if you have any questions, please feel free to drop a comment.

When we run these tests we’ll get this output: