TestNG Tutorials 21: Why Don’t We Require a Main Method In TestNG class For Execution Of Methods?

Hello Folks,

This question was asked to me by one of my readers. Actually this is an interesting topic and thought of sharing knowledge through a post.

Basic Java concept says, we need a main method to execute a java class or execution of a class starts from its main method. But in case of TestNG class, we do not write any main method. We generally run a TestNG class as a TestNG test or through testng.xml. We do not require to create a main method in it. We will try to understand reason behind this in this post.

We use different types of annotation in TestNG class for different purpose. Each annotation is self explanatory and usage. For example: If we mark a method as @Test or @BeforeClass etc then TestNG will execute those methods in some order based on annotations provided.

For better understanding, We will learn basics of Annotation in Java.

Annotation:

Java annotation is a tag, which provides metadata for your java code  to indicate some additional information which can be used by java compiler or JVM.  I can give you a real time example which we do in regular testing work. When we write test cases, generally we label them as Smoke, Regression or based on some module name which provides extra information about that particular test case. In same way, annotation works in java.

We have so many built-in annotations in Java. Some are as below:

@Override: The @Override Java annotation is used above methods that override methods in a superclass. If the method does not match a method in the superclass, the compiler will give you an error.

@SuppressWarnings: The @SuppressWarnings annotation makes the compiler suppress warnings for a given method.

We can even create our own annotation as we create class or interface in a file. An example is below:

In above example, I created a custom annotation called “MyAnnotation ” with three elements. We need to use @interface to let compiler know that it is a custom annotation. Elements inside annotation definition let you allow to pass values to be used while execution or it can change the flow of execution. Like “priority” field in @Test annotation.

In the same way, TestNG has also defined an annotation Test as below:

If you see carefully, you can find elements as “enabled”, “dependsOnGroups” and “invocationTimeOut” etc which we generally assign a value with @Test annotation. Now at least you get an idea from where those elements are coming.

You can see two annotations on declaration of Test annotation @Retention(RetentionPolicy.RUNTIME) and @Target(value{METHOD,TYPE}). @Retention says this it should be available at runtime, for inspection via reflection. @Target says on which Java elements your custom annotation can be used to annotate. Based on annotation, TestNG decides how a method will be executed.

Like @Test annotations, TestNG also defined other annotations. Every annotations in TestNG has their own significance and serves different purpose. When we create and run a testng.xml or run using run() method of TestNG class, it internally builds a suite of methods to be executed based on annotation provided to methods. You can find a sample code below from TestNG:

TestNG.class contains “main()” actually which calls suite internally to execute. You can find code below:

So, you do not need to write main() method in a TestNG class to run it as TestNG takes care of that by defining annotations. You just need to provide proper annotations to methods and rest TestNG will execute them in a manner by implicit call to main method of TestNG class. I hope you would have got basics of annotations and how TestNG does not require main method to run it.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Leave a Reply

Your email address will not be published. Required fields are marked *