Spring Boot TestNG Tutorial


TestNG is a powerful testing framework inspired by JUnit and NUnit, designed to simplify a broad range of testing needs, from unit testing to integration testing. It introduces new functionalities that make it more powerful and easier to use, such as annotations, flexible test configurations, test groups, and parallel execution. This tutorial will demonstrate how to integrate TestNG into a Spring Boot application with step-by-step examples.

To learn more about TestNG, check out this guide: TestNG.


  1. Java Development Kit (JDK) 17 or later
  2. Apache Maven installed
  3. An IDE like IntelliJ IDEA or Eclipse

Step 1: Create a Spring Boot Project

You can create a Spring Boot project using Spring Initializr or your IDE.

Using Spring Initializr

  1. Go to Spring Initializr.
  2. Select the following options:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 3.0.0 or later
    • Group: com.example
    • Artifact: testng-demo
    • Name: testng-demo
    • Package name: com.example.testngdemo
    • Packaging: Jar
    • Java: 17 or later
  3. Add the following dependencies:
    • Spring Web
    • Spring Data JPA
    • H2 Database
  4. Click "Generate" to download the project zip file.
  5. Extract the zip file and open the project in your IDE.

Step 2: Add TestNG Dependency

Add the following dependencies to your pom.xml file:

    <!-- Spring Boot Starter Web -->

    <!-- Spring Boot Starter Data JPA -->

    <!-- H2 Database -->

    <!-- Spring Boot Starter Test -->

    <!-- TestNG -->

Step 3: Configure Application Properties

Add the following properties to src/main/resources/application.properties:


Step 4: Create Entity and Repository Classes

Create the Employee Entity

Create a new Java class named Employee in the com.example.testngdemo package:

package com.example.testngdemo;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

public class Employee {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;

    // Getters and Setters
    public Employee() {}

    public Employee(String name, String email) {
        this.name = name;
        this.email = email;

    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public String getEmail() {
        return email;

    public void setEmail(String email) {
        this.email = email;

Create the Employee Repository

Create a new Java interface named EmployeeRepository in the com.example.testngdemo package:

package com.example.testngdemo;

import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {

Step 5: Create the Service Class


Create a new Java class named EmployeeService in the com.example.testngdemo package:

package com.example.testngdemo;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

public class EmployeeService {

    private final EmployeeRepository employeeRepository;

    public EmployeeService(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;

    public List<Employee> findAll() {
        return employeeRepository.findAll();

    public Optional<Employee> findById(Long id) {
        return employeeRepository.findById(id);

    public Employee save(Employee employee) {
        return employeeRepository.save(employee);

    public void deleteById(Long id) {

Step 6: Create the Controller Class


Create a new Java class named EmployeeController in the com.example.testngdemo package:

package com.example.testngdemo;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class EmployeeController {

    private final EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;

    public List<Employee> getAllEmployees() {
        return employeeService.findAll();

    public ResponseEntity<Employee> getEmployeeById(@PathVariable Long id) {
        return employeeService.findById(id)

    public Employee createEmployee(@RequestBody Employee employee) {
        return employeeService.save(employee);

    public ResponseEntity<Employee> updateEmployee(@PathVariable Long id, @RequestBody Employee employee) {
        return employeeService.findById(id)
                .map(existingEmployee -> {
                    return ResponseEntity.ok(employeeService.save(employee));

    public ResponseEntity<Void> deleteEmployee(@PathVariable Long id) {
        return employeeService.findById(id)
                .map(employee -> {
                    return ResponseEntity.noContent().build();

Step 7: Create the Main Application Class

Create a main application class named TestngDemoApplication in the com.example.testngdemo package:

package com.example.testngdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class TestngDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestngDemoApplication.class, args);

Explanation: The TestngDemoApplication class contains the main method, which is the entry point of the Spring Boot application. The @SpringBootApplication annotation is a convenience annotation that adds all the following:

  • @Configuration: Tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  • @ComponentScan: Tells Spring to look for other components, configurations, and services in the specified package.

Step 8: Create TestNG Test Classes

Test the EmployeeService

Create a new test class named EmployeeServiceTest in the src/test/java/com/example/testngdemo package:

package com.example.testngdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Optional;

import static org.testng.Assert.*;

public class EmployeeServiceTest extends AbstractTestNGSpringContextTests {

    private EmployeeService employeeService;

    public void testFindAll() {
        List<Employee> employees = employeeService.findAll();

    public void testSave() {
        Employee employee = new Employee("Amit Sharma", "[email protected]");
        Employee savedEmployee = employeeService.save

        assertEquals(savedEmployee.getName(), "Amit Sharma");

    public void testFindById() {
        Employee employee = new Employee("Rohit Sharma", "[email protected]");
        Employee savedEmployee = employeeService.save(employee);
        Optional<Employee> foundEmployee = employeeService.findById(savedEmployee.getId());
        assertEquals(foundEmployee.get().getName(), "Rohit Sharma");

    public void testDeleteById() {
        Employee employee = new Employee("Suresh Kumar", "[email protected]");
        Employee savedEmployee = employeeService.save(employee);
        Optional<Employee> foundEmployee = employeeService.findById(savedEmployee.getId());


  • @SpringBootTest: Used to load the complete application context.
  • extends AbstractTestNGSpringContextTests: Integrates TestNG with Spring Boot.
  • @Test: Indicates a TestNG test method.

Step 9: Run the Tests

You can run the tests using your IDE or by executing the following command in the terminal:

mvn test

You should see an output indicating that all tests have passed successfully.


In this tutorial, we demonstrated how to integrate TestNG into a Spring Boot application and covered various use cases, including complex examples. We created a simple Spring Boot application with Employee entity, repository, service, and controller layers, and wrote TestNG test cases to test the service layer. 

By following these steps, you can efficiently use TestNG to test your Spring Boot applications, ensuring robust and reliable code.