Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Integration of Maven with TestNG

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Hello Ninjas! In this article, we will learn about the Integration of Maven with TestNG. We are going to start with understanding what maven and TestNG are and why there is a need to integrate Maven with TestNG.                              

Integration of Maven with TestNG

About Maven

Maven is an open-source build automation tool developed by Apache Group for better project management. It is written in Java to create projects in languages such as C#, Scala, Ruby, etc. Maven helps developers to build, publish and develop projects, dependencies, and documentation.                                          

Maven
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

About TestNG

It is a testing framework that is designed for the Java programming language. TestNG covers many test categories with powerful and user-friendly functions such as unit, functional, end-to-end, integration, etc.

Basic Terminologies 

  • Maven Surefire Plugin: Maven Surefire plugin is used to execute an application's unit tests during the build lifecycle test phase.
     
  • Maven Central Repository is a space where all the libraries and plugins are stored. 
     
  • POM (Project Object Model): Project Object Model is an XML file and a basic work unit in Maven. It carries information about the project and the configuration details used by Maven during the process.


Before proceeding with the setup procedure, let us look at why there is a need to integrate Maven with TestNG -

  • We can not select a specific test suite to execute from the list of available suites without integrating Maven and TestNG.
     
  • We can not manage our dependencies in TestNG without integrating it with Maven.
     

Prerequisites

  • Install Maven in your eclipse.
     
  • Internet Connection
     
  • Access to a laptop/desktop

Integrating Maven with TestNG

Step 1: Choose a POM.xml file from the maven Project < Right-click < Maven < Add plugin.

Integration of Maven with TestNG(Step 1)

Step 2: The “Add Plugin” window will be displayed

Steps to enter the plugin details -

  • Click here and look for the “Using TestNG” option on the left side of the window.
     
  • Select the XML code displayed under the “Using Suite XML File” header < Enter Group ID, Artifact ID, and Version details in the “Add Plugin” window using the XML code snippet given below
     
  • Click Okay

Integrating Maven with TestNG(Step 2)

Now add

  • groupId = org. Maven.plugins
  • artifactId = maven-surefire-plugin
  • version = 3.0.0-M7
Integration of Maven with TestNG(Step 2 continued)

Step 3: Click Okay. The plugin is now added to the selected pom.xml file.

Integration of Maven with TestNG(Step 3)

Execute a Test Suite using Maven Surefire Plugin.

Right-click on your java program file in the package explorer and select TestNG > Convert to TestNG.

How to Execute a Test Suite using Maven Surefire Plugin(Step 1)

In the Refactoring window, click on “Next.”

How to Execute a Test Suite using Maven Surefire Plugin(Step 2)

Now click on “Finish”. It will create a testng.xml file.

How to Execute a Test Suite using Maven Surefire Plugin(Step3)

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="javaPackageName.ClassName"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->


Now add the following line in the pom.xml file between </version> and </plugin>.

<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>


Now it will look like the image shown below.

Source code

Now right-click on pom.xml file and select Run As6 Maven test.

Step 5

The output will look like below-given image and we will get a “Build Success” message.

Step 6

Frequently Asked Questions

What is Maven?

Maven is an open-source build automation tool developed by Apache Group for better project management.

What is TestNG?

TestNG is a testing framework designed for Java programming language.

What is Eclipse IDE?

Eclipse is an integrated development environment for projects in several computer programming languages.

What is the Maven Surefire Plugin?

The maven Surefire plugin is used to execute the unit tests of applications during the test phase of the build lifecycle.

What does POM stand for?

POM stands for Project Object Model. It is a basic work unit in Maven.

Conclusion

In this article, we learned about Maven, and TestNG, why there is a need to integrate Maven with TestNG, and how to integrate Maven with TestNG.

If you want to dig deeper into Maven, here are some related articles - 

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practise our available mock tests, look at the interview bundle for interview preparations, and so much more!

Topics covered
1.
Introduction
2.
About Maven
3.
About TestNG
3.1.
Basic Terminologies 
4.
Integrating Maven with TestNG
5.
Execute a Test Suite using Maven Surefire Plugin.
6.
Frequently Asked Questions
6.1.
What is Maven?
6.2.
What is TestNG?
6.3.
What is Eclipse IDE?
6.4.
What is the Maven Surefire Plugin?
6.5.
What does POM stand for?
7.
Conclusion