Creating your own TestNG Annotation

TestNG has many Annotations like @Test, @DataProvider, @BeforeTest, @AfterTest, etc.

In most cases, these Annotations will suffice the user’s needs, but what if you need a specific runtime behavior for your tests and none of these Annotations offer that behavior?

Well, you can write your own Annotation for that and have your Annotation processed at runtime like other TestNG Annotations.

Here is how you do it -

  • Create a basic maven project and add TestNG as a dependency.
  • Create your Annotation
  • Create your Listener
  • Create a folder called META-INF\services in src\main\resources
  • Create a file called org.testng.ITestNGListener in src\main\resources\META-INF\services
  • Add the following line to this file - com.amareshp.annotations.MyTestNGAnnotationListener - (Note: You may have to leave a blank line below this line).

To see a complete example - fork the project - https://github.com/amareshp/testng-examples

Custom Annotation

Listener

To learn more about Java’s Service Provider Interface (SPI) -

Java SPI

You can also use the SPI project’s @ProviderFor annotation to avoid creating necessary files for the SPI to work -

SPI Project

Written on October 15, 2014