Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/19/2025 By admin

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

Uncategorized

Post navigation

Previous Post: Postman Tutorial Part 7 – Difference Between ‘Accept’ and ‘Content-Type’ HTTP Headers
Next Post: Postman Tutorial Part 5 – Sending POST Request in Postman

Related Posts

TestNG Tutorials 23: @Test Annotation – Don’t Confuse TestNG With Duplicate Priorities Uncategorized
scrollBy500 – Make Selenium Easy Uncategorized
July 2018 – Page 2 – Make Selenium Easy Uncategorized
September 29, 2019 – Make Selenium Easy Uncategorized
April 26, 2017 – Make Selenium Easy Uncategorized
ParamsInPostman – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark