16.1 Annotation

An annotation is a form of metadata that provides data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code which is annotated. The annotation has a number of uses and few of them are as follows: 

  • Annotation can be used by the compiler to detect errors or suppress warnings.
  • Annotations can be examined at runtime.
  • Software tools can be written that process annotation information to generate code, XML files, etc.

An important use of annotation in test automation is to define the test type (functional, regression) and test functional area (customer accounts, funds transfer, etc., for a banking application). Once this information is defined as an annotation in tests, the tool can be written to selectively execute a specific type of test. Also, the tool can provide info about the test coverage in each functional area.

16.1 | Built-in Annotations

Java provides some built-in annotations. Few of them are listed below:

  • @Override: Verifies that the specific method is an override. It causes a compile error if the method is not found in one of the parent classes or implemented interfaces.
  • @Deprecated: Marks the specific method as obsolete. It causes a compile-time error if the specific method is used.
  • @SuppressWarnings: Instructs the compiler to suppress the compile-time warnings which match the annotation parameters.

Section 'Interfaces' shows the use of @Override annotation.

Annotations can also be used to annotate annotations. Listed as follows are few annotations that define the behavior of an annotation:

  • @Retention: Specifies how the annotation is stored- whether in code only or compiles into the class or available at runtime through reflection.
  • @Documented: Marks another annotation for inclusion in the documentation.
  • @Target: Marks another annotation to restrict the kind of java elements the annotation may be applied to.
  • @Inherited: Marks another annotation to be inherited to a subclass of annotated class.

No comments: