Generating Unit-Test-Stubs for Java using Annotation Processing
The idea is to improve unit test coverage without implementing to much boilerplate code. The delevoper doesn't care anymore, whether to write a unit test or not. Now he explicitly decides against writing an unit test, if it doesn't make sense.
- Add the
annotation-processor
to yourmaven-compiler-plugin
in your pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>de.nms.test</groupId>
<artifactId>annotation-processor</artifactId>
<version>${latest.annotation-processor.version}</version>
</path>
</annotationProcessorPaths>
<annotationProcessors>
<proc>de.nms.test.apt.processor.TestingAnnotationProcessor</proc>
</annotationProcessors>
</configuration>
</plugin>
</plugins>
</build>
- Add the
annotations
-dependency to your project:
<dependencies>
<dependency>
<groupId>de.nms.test</groupId>
<artifactId>annotations</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>de.nms.test</groupId>
<artifactId>annotations</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
- Create a java class annotated with
@GenerateTestStub
. If you're looking for examples, please have closer look to samples. - There will be an unit test class automatically generate for every
public
and non-abstract
method implemented in that class.
shell> mvn clean compile
- To avoid generating a test for a single method you'll be able to add the
@GenerateNoTestStub
to the method itself.