Spring Boot TestNG Example (2024)
In this tutorial, we will learn how to implement TestNG with Spring Boot application.
TestNG Tutorial :
Q: What is the TestNG?
Ans:
- In TestNG, NG is for Next Generation. TestNG is an open-source automated TestNG framework.
- When actual development of the framework from scratch level is done TestNG framework plays a very important role.
- The limitations of the older framework were removed by the TestNG Framework.
- TestNG gave the ability to write flexible and significant tests to the developers with the help of annotations, sequencing, parametrizing, and grouping.
Create Spring Boot Project from Spring Initializer
Project Structure
Maven Dependency
All we need is spring-boot-starter-web
dependency, org.testng
for testng, add
org.projectlombok
for auto generating getters/setters/constructor.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.techgeeknext</groupId>
<artifactId>Spring-Boot-Testng-Example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring-Boot-Testng-Example</name>
<description>Spring Boot Testng Example</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Rest Controller
Create rest endpoint to test the method using testNG.
package com.techgeeknext.controllers;
import com.techgeeknext.model.Employee;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmployeeController {
/**
* Get the employee by id
*
*/
@GetMapping("/employee")
public Employee getEmployee() {
return Employee.builder()
.id(1)
.name("TechGeekNextUser")
.role("Admin")
.build();
}
}
Employee Model Object
package com.techgeeknext.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Employee {
private long id;
private String name;
private String role;
}
Test rest endpoint using TestNG
Create Test class to test employee rest endpoint using TestNG.
package com.techgeeknext.controller;
import com.techgeeknext.SpringBootTestngExampleApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest(classes = SpringBootTestngExampleApplication.class)
public class EmployeeControllerTests extends AbstractTestNGSpringContextTests {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@BeforeClass
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build();
}
@Test
public void testGetEmployee() throws Exception {
mockMvc.perform(get("/employee"))
.andExpect(status().isOk())
.andExpect(content()
.contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("TechGeekNextUser"))
.andExpect(jsonPath("$.role").value("Admin"));
}
}
Run the TestNG Test Class
- Start the Spring Boot Application by running
spring-boot:run
or by running main class. - Run the TestNG Class : Right click on the project -> select Run -> Select All Tests option
- TestNG Test Output:
Download Source Code
The full source code for this article can be found on below.
Download it here -
Spring
Boot TestNG Example