8 Excellent Advantages of Annotations

·        Test cases can be grouped more easily.
  • Parallel testing is possible .
  • Annotations play an important role in execution. They guide as to which Class or method will run first and which next .
  • Have a look at the benefits of annotations below:
  • Test classes do NOT need to extend anything  Test Case class as for JUnit 3.
  • TestNG identifies the methods by looking up annotations . Which means method names will not require a pattern or format.
  • We can pass additional parameters to annotations.
  • Annotations are strongly typed, so the compiler will flag any mistakes right away for verification
As stated earlier , annotations are special information about text or program. So  they act accordingly , based on the information that they provide. Take for example 
Built In annotation such as @SuppressWarnings . This is used to suppress the warnings that appear at compilation of the program. So when we annotate a method in Java with @SuppressWarnings, then that means the compile time warnings that we specify in the parameters of that annotation will be suppressed for that method.

Examples : 
·         SuppressWarnings("parameters here")
·         Static-access
Static methods are reffered by Classname.methodname
we cannot create a instance of class to refer to a static method.
That will throw a compilation error The static method " "  should be accessed in a static way.”
public class Welcome{
public static void method1() {
}
public static void main(String[] args) {
Welcome.method1  // this call to static method1 will not give compile error .
Welcome Wel= new Welcome();
Wel.method1 (); // this call to static method1 will give compile error , because static methods should be called directly by class names.
}
}

For suppressing such errors we use "static-access" parameter as follows before the method implementation.
·         @SuppressWarnings("static-access")
 public void MethodName(String ParameterName) throws InvalidFormatException  {
}
·         @Override annotation , which is again a Built In annotation , when put before a method tells the compiler that the following method is overriding its base class method. And it will throw a compilation error , if there does not exist such a method in parent class .
ex.  @Override 
protected abstract void method1();

·         @Deprecated , also a Built In annotation , tells the compiler that the following method is Obsolete or out of date. so compiler will throw an warning message if we try to use this method anywhere in the code.

Previous                                                                  Next