Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 13: Can An Interface have TestNG Methods? If Yes, How To Execute Them?

Posted on 03/21/2025 By admin

Hello Folks,

We talked about TestNG class in previous post. Can we declare and define TestNG annotated methods in an Interface? We will learn now.

As per Java 8, An interface can have abstract methods, default methods and static methods.

Let’s create an Interface with TestNG annotated abstract methods and observe what happens:

An interface with TestNG annotated methods:

You can see there is no error messages. It means that we can have TestNG annotated methods in an interface.

Let’s create a testng.xml for above interface:

You can see that testng.xml is generated by putting interface name “InterfaceTestNGMethods” in class tag. So, you can generate a testng.xml for an interface which has TestNG @Test annotated method.

Now let’s run it as testng test:

You can see count of tests run is zero. Let’s assume as of now that @Test annotated method is an abstract method in interface so that is not included in test run.

Now, add a default and static @Test annotated method in interface and run it.

Updated Interface:

There is no need to regenerate testng.xml. We run same previously generated testng.xml and observe output:

Again same output as previous run. This time you have concrete Test annotated methods but it is not run by TestNG.

Let’s create an implemented class of above interface.

I added a new @Test method in driver class so that testng.xml can include this class otherwise you need to manually add class name in testng.xml.

Regenerate testng.xml:

Console output:

Finally it ran all Test annotated method.

Let’s update Interface as below and run:

Output:

You can notice all methods ran successfully.

So, an interface can have TestNG annotated methods in it but you require an implemented class of interface to run it.

Hope you learnt new thing today.

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: How To Sort List In Ascending & Descending Order Using Java Stream API?
Next Post: Learn Selenium With Quiz – Basic Level 8 – CSS Selector

Related Posts

Usage of Java Stream API in Selenium – Retrieving Text From a List Of WebElements Uncategorized
consoleInFirefox – Make Selenium Easy Uncategorized
REST Assured Tutorial 38 – How Getter & Setter methods Matter For Serialization Deserialization Using POJO Uncategorized
Postman Tutorial Part 50 – How to Retrieve Request Body in Postman Uncategorized
FrameFirefox – Make Selenium Easy Uncategorized
July 29, 2017 – 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