Setting up TestNG takes two minutes. You add a dependency to pom.xml, and you are ready to write tests. No manual JAR downloads, no classpath headaches.
Open your pom.xml and add these dependencies. If you already have the Selenium dependency from the previous chapters, just add TestNG.
<dependencies>
<!-- Selenium WebDriver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.18.1</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>Notice the <scope>test</scope> tag on TestNG. This means TestNG is only available in the src/test/java folder. It will not be bundled with your production code. This is the correct way to add any test dependency.
Maven Surefire is the plugin that actually runs your tests. Without it, "mvn test" will not know to use your testng.xml file.
<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>Maven expects your test classes in src/test/java. This is not optional — if you put tests in src/main/java, Maven will not find them.
my-automation-project/
├── pom.xml
├── testng.xml
├── src/
│ ├── main/java/ ← Page objects, utilities, base classes
│ │ └── com/autopractice/
│ │ ├── pages/
│ │ ├── utils/
│ │ └── base/
│ └── test/java/ ← Test classes go HERE
│ └── com/autopractice/
│ └── tests/
│ ├── BankingLoginTest.java
│ ├── ShoppingCartTest.java
│ └── InsuranceQuoteTest.java
└── test-output/ ← TestNG generates reports hereimport org.testng.Assert;
import org.testng.annotations.Test;
public class FirstTest {
@Test
public void testAddition() {
int result = 2 + 3;
Assert.assertEquals(result, 5, "2 + 3 should equal 5");
System.out.println("Test passed!");
}
@Test
public void testStringContains() {
String title = "Banking Portal - Login";
Assert.assertTrue(title.contains("Banking"),
"Title should contain 'Banking'");
}
}Right-click the file in IntelliJ and select "Run FirstTest." You should see green checkmarks next to both tests. If you see red, check your pom.xml dependencies.
If IntelliJ does not recognize @Test or Assert, make sure you imported from org.testng, not from org.junit. Both frameworks have @Test and Assert classes — using the wrong import will cause confusing errors.
Q: How do you set up TestNG in a Maven project?
A: Three steps: (1) Add the TestNG dependency in pom.xml with <scope>test</scope>. (2) Add the Maven Surefire plugin with the testng.xml file path in its configuration. (3) Create test classes in src/test/java and annotate test methods with @Test. Run with mvn test from the command line or right-click in the IDE.
Key Point: Add TestNG dependency + Maven Surefire plugin to pom.xml. Test classes go in src/test/java.