#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
TestNG & Maven

What is Maven

Definition

Maven is a build automation and dependency management tool for Java projects, using a standard project structure and an XML configuration file called pom.xml.

Key Benefits

  • Automatically downloads and manages project dependencies (like Rest Assured, TestNG) from a central repository.
  • Provides a standard, predictable project structure across teams.
  • Supports build lifecycle phases: compile, test, package, and more.
  • Integrates cleanly with CI/CD tools like Jenkins.

A pom.xml for a Selenium and TestNG Project

Everything Maven does is driven by the pom.xml file at the root of the project. Its coordinates (groupId, artifactId and version) identify your project, and the dependencies section lists the libraries it needs. A typical test automation project looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.qa</groupId>
  <artifactId>web-tests</artifactId>
  <version>1.0.0</version>

  <properties>
    <maven.compiler.release>17</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.25.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.10.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

The version numbers above are only examples; check Maven Central for the current release of each library before you copy them. The Surefire plugin is what actually runs your tests during the test phase, and the configuration tells it to use your TestNG suite file.

Standard Folder Layout and the Build Lifecycle

Maven expects code in fixed places, which is why any Maven project feels familiar the moment you open it:

FolderHolds
src/main/javaApplication or framework code, such as page objects and utilities
src/test/javaTest classes
src/test/resourcesTest data, config files and suite files
targetBuild output and reports, created by Maven and never committed

The default lifecycle runs in order: validate, compile, test, package, verify, install, deploy. When you ask for a phase, Maven runs every phase before it as well, so mvn package also compiles and tests.

mvn clean test                      # delete old output, compile, run all tests
mvn test -Dtest=LoginTest           # run a single test class
mvn test -DskipTests                # build without running tests
mvn dependency:tree                 # show every library and where it came from

Downloaded libraries are cached in a local repository, usually the .m2 folder in your home directory, so the second build is much faster than the first.

Mistakes Testers Make with Maven

  • Wrong dependency scope. Test libraries should use test scope. But if your page objects live in src/main/java and use Selenium, Selenium needs the default compile scope.
  • Tests that only run from the IDE. If mvn clean test fails on the command line, it will fail on Jenkins too. Run it locally before pushing.
  • Version conflicts. Two libraries can pull in different versions of the same dependency. Use mvn dependency:tree to find the clash.
  • Committing the target folder. Add it to .gitignore; Maven recreates it on every build.
  • Hard-coding the browser or URL. Pass values such as -Dbrowser=chrome on the command line and read them with System.getProperty(), so one build can target different environments.

Gradle is the main alternative to Maven. It uses a script-based build file and is popular in Android projects, while Maven's fixed conventions remain very common in Java test automation teams.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course