Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write automated unit tests for java annotation processor?

I'm experimenting with java annotation processors. I'm able to write integration tests using the "JavaCompiler" (in fact I'm using "hickory" at the moment). I can run the compile process and analyse the output. The Problem: a single test runs for about half a second even without any code in my annotation processor. This is way too long to using it in TDD style.

Mocking away the dependencies seems very hard for me (I would have to mock out the entire "javax.lang.model.element" package). Have someone succeed to write unit tests for an annotation processor (Java 6)? If not ... what would be your approach?

like image 780
Arne Deutsch Avatar asked Nov 28 '09 08:11

Arne Deutsch


People also ask

Which annotation is used for unit testing?

A JUnit test is a method contained in a class which is only used for testing. This is called a Test class. To define that a certain method is a test method, annotate it with the @Test annotation. This method executes the code under test.

Which of the following annotation can be used to declare a test method?

Summary. JUnit is a framework which supports several annotations to identify a method which contains a test. JUnit provides an annotation called @Test, which tells the JUnit that the public void method in which it is used can run as a test case.


1 Answers

This is an old question, but it seems that the state of annotation processor testing hadn't gotten any better, so we released Compile Testing today. The best docs are in package-info.java, but the general idea is that there is a fluent API for testing compilation output when run with an annotation processor. For example,

ASSERT.about(javaSource())     .that(JavaFileObjects.forResource("HelloWorld.java"))     .processedWith(new MyAnnotationProcessor())     .compilesWithoutError()     .and().generatesSources(JavaFileObjects.forResource("GeneratedHelloWorld.java")); 

tests that the processor generates a file that matches GeneratedHelloWorld.java (golden file on the class path). You can also test that the processor produces error output:

JavaFileObject fileObject = JavaFileObjects.forResource("HelloWorld.java"); ASSERT.about(javaSource())     .that(fileObject)     .processedWith(new NoHelloWorld())     .failsToCompile()     .withErrorContaining("No types named HelloWorld!").in(fileObject).onLine(23).atColumn(5); 

This is obviously a lot simpler than mocking and unlike typical integration tests, all of the output is stored in memory.

like image 119
gk5885 Avatar answered Sep 25 '22 13:09

gk5885