Spring Boot Junit Example (2024)
In this tutorial, we'll demonstrate how to create a Spring Boot Junit example for simple GET rest api.
Create Spring Boot application
Create Spring Boot application from Spring Initializr.
Project Structure
Add Dependencies
Add below dependencies for Web - spring-boot-starter-web
, for junit starter - spring-boot-starter-test
and lombok - lombok
for auto getters/setters in pom.xml
.
<?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>SpringBootJunitExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootJunitExample</name>
<description>Spring Boot Junit 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.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>
Take a look at our suggested posts:
Data Model
Create Employee
class, contains id
, name
and
role
.
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;
}
Employee Controller to handle GET rest endpoint
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 {
@GetMapping("/employee")
public Employee getEmployee() {
return Employee.builder().id(1).name("TechGeekNext").role("Admin").build();
}
}
Spring Boot Junit Employee Test Class
- Create
EmployeeControllerTest
class under thetest/java
folder which will call theGET
rest endpoint and test the expected response values. - There are many ways for unit testing APIs. Spring Boot, JUnit, MockMvc, and Mockito are my preferred combination because they are all open-source and support Java, which is our primary language.
- A Runner class is in charge of running JUnit tests, generally using reflection.
@RunWith(EmployeeController. class)
is an example of@RunWith
, which defines a set of several test classes to run alongside the class where it is used. MockMVC
is a Spring MVC test framework class that assists in testing controllers by directly creating a Servlet container.- Create a
MockMvc
instance with the provided fully initializedWebApplicationContext
.
package com.techgeeknext.test;
import com.techgeeknext.controllers.EmployeeController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@WebMvcTest(EmployeeController.class)
public class EmployeeControllerTest {
@Autowired
private WebApplicationContext applicationContext;
@Autowired
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
}
@Test
public void getEmployeeTest() throws Exception {
mockMvc.perform(get("/employee"))
.andExpect(status().isOk())
.andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.id").value("1"))
.andExpect(jsonPath("$.name").value("TechGeekNext"))
.andExpect(jsonPath("$.role").value("Admin"));
}
}
Test Spring Boot Junit Example
- Start the Spring Boot Application by running
spring-boot:run
or by running main class. Test Junit Class
Run the test case from theEmployeeControllerTest.java
class by right-clicking and selecting"Run getEmployeeTest()"
and we can see that the test case was successfully executed.
Download Source Code
The full source code for this article can be found below.
- Download it here - Spring Boot Junit Example