testinganswers.com

16.2 Annotation

16.2 Annotation

16.2 | Custom Annotations

Java provides the flexibility to define custom annotations as per need. An annotation is defined with the at-sign (@) preceding the interface keyword. The following steps show how to create and use custom annotations.

  • Open Eclipse
  • Select the package where annotation needs to be created.
  • Right-click and select New → Annotation as shown in figure
 
Creating a new annotation file
Creating a new annotation file
  • Specify annotation name say RegressionTest.
The following code shows the annotation code with no annotations and parameters.
 
RegressionTest
RegressionTest
Above annotation can be directly used in code as:
 
UsingAnnotation
Use of Annotation
  • Annotate the annotation as required.
The following code shows how to create an annotation that defines the test type being automated (functional or regression). This annotation also defines the retention policy and the applicability of the TestDetails annotation.
 
Annotate the Annotation
 
  • Define the parameters of annotation, if any.
The following code below shows how to define two parameters for test information test developed by and test modified by.
 
Parameters of annotation
Parameters of annotation
 
  • Use the annotation while developing code wherever required.
The following code uses the annotation TestDetails for test ‘testPurchaseOrder‘.
annotation
Use of Annotation
 

6 thoughts on “16.2 Annotation

Leave a Reply

%d bloggers like this: