How to write automated unit tests for java annotation processor?

前端 未结 6 1389
遇见更好的自我
遇见更好的自我 2021-01-31 09:24

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

6条回答
  •  無奈伤痛
    2021-01-31 09:58

    jOOR is a small Java reflection library that also provides simplified access to the in-memory Java compilation API in javax.tool.JavaCompiler. We added support for this to unit test jOOQ's annotation processors. You can easily write unit tests like this:

    @Test
    public void testCompileWithAnnotationProcessors() {
        AProcessor p = new AProcessor();
    
        try {
            Reflect.compile(
                "org.joor.test.FailAnnotationProcessing",
                "package org.joor.test; " +
                "@A " +
                "public class FailAnnotationProcessing { " +
                "}",
                new CompileOptions().processors(p)
            ).create().get();
            Assert.fail();
        }
        catch (ReflectException expected) {
            assertFalse(p.processed);
        }
    }
    

    The above example has been taken from this blog post

提交回复
热议问题