Giter Club home page Giter Club logo

annotation-processor-testing's Introduction

annotation-processor-testing

An easy way to verify that your annotation processor is emitting the right diagnostics.

Setup

In order to use any of the test runners, you must include your test sources as a resource:

<build>
...
<testResources>
  <testResource>
    <directory>src/test/java/</directory>
  </testResource>
  <testResource>
    <directory>src/test/resources/</directory>
  </testResource>
</testResources>
...
</build>

Additionally, you may need to disable annotation processing during test compilation if you are testing ERROR diagnostics.

<build>
...
<plugins>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <executions>
   <execution>
     <id>default-testCompile</id>
     <goals>
       <goal>testCompile</goal>
     </goals>
     <configuration>
       <proc>none</proc>
     </configuration>
   </execution>
 </executions>
</plugins
...
</build>

Usage

Annotate your test class with @UseProcessor, @ExpectError, and @ExpectDiagnostic:

@UseProcessor(YourProcessor.class)
public class YourProcessorTest {
  @ExpectError @IncorrectlyUsedAnnotation
  public void method() {}

  @ExpectError("substring of the message") @IncorrectlyUsedAnnotation
  public void method1() {}

  @ExpectError(lineOffset = 1) // Expect a diagnostic on the next line.
  @IncorrectlyUsedAnnotation
  public void method2() {}

  @ExpectError(value = "[abc]+", regex = true) @IncorrectlyUsedAnnotation
  public void method3() {}

  @ExpectError(testName = "descriptive JUnit test name") @IncorrectlyUsedAnnotation
  public void method4() {}

  // No @Expect annotation needed.
  @CorrectlyUsedAnnotation
  public void method5() {}
}

JUnit 4

Maven Central

Add @RunWith(DiagnosticTester.class) to your test class.

JUnit 5

Maven Central

Ensure that annotation-processor-testing-junit5 is included as a dependency, and the JUnit 5 launcher should automatically discover your tests.

annotation-processor-testing's People

Contributors

jefffaer avatar

Stargazers

Cristiano Gavião avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.