Categories: Selenium TopicsTestNG Tutorials

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

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

 

Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Recent Posts

Java Programs 17: Java Program to Find Duplicate Elements in Each Row of 2D Array

Hello Programers, Problem: How to find duplicate values in each row of 2D array. For example , we have 2d…

13 hours ago

Best Practises in Page Object Model : Naming Conventions of Web Elements & Actions on it

Hello Folks, Recently I have started learning or exploring more on best practises in Selenium Framework and same I want…

2 days ago

TestNG Tutorials 41: Skipping a Test Conditionally in TestNG

Hello Folks, In this post we will learn below topics: 1. How to skip a test conditionally? 2. How conditionally…

2 days ago

TestNG Tutorials 40: Groups of Groups or MetaGroups in TestNG

Hello Folks, Grouping is a best feature provided by TestNG. We group test scripts based on suite name or functionality…

4 days ago

TestNG Tutorials 39: Run Only Test From a TestNG Class Without Using TestNG XML?

Hello Folks, Suppose you have created a testng class which has many @Test annotated methods say 10 test methods. You…

7 days ago

TestNG Tutorials 38: How to Execute Groups in TestNG without Using TestNG XML

Hello folks, In this post, we will see something new related to grouping in TestNG. When we assign group or…

1 week ago