VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/unit-testing-in-spring-boot-project-using-mockito-and-junit/

⇱ Unit Testing in Spring Boot Project using Mockito and Junit - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Unit Testing in Spring Boot Project using Mockito and Junit

Last Updated : 3 Oct, 2025

Unit testing is a practice in software development, ensuring that individual components of an application work correctly. In Spring Boot projects, Mockito and JUnit are used to write clean, maintainable unit tests.

Technologies used

  • Spring Boot: Java framework for rapid application development.
  • JUnit 5: Popular testing framework for Java applications.
  • Mockito: A Mocking framework that simulates dependencies, allowing testing in isolation.

Annotations and Testing Utilities

  • @Mock: Creates mock objects to simulate real ones during testing using Mockito.
  • @InjectMocks: Injects mock dependencies into the object under test.
  • assert Methods: Used in testing to verify expected outcomes like equality, null or exception checks.

Step-by-Step Implementation

Step 1: Create a Spring Boot project.

We can create a Spring Boot Project with IntelliJ IDEA or Spring Initializr. Include the following dependencies:

  • Spring Web
  • MySQL Database
  • Lombok
  • Spring Data JPA

Example: pom.xml File

Step 2: Project Structure

Create the packages and files as seen in the below image. Below is the complete file structure of this project.

👁 Project Flow

Step 3: Create Entity Class

It is done via creating a simple POJO class inside the Person.java file.

Step 4: Create Repository Interface

Create a simple interface and name the interface as PersonRepo. This interface is going to extend the JpaRepository.

Note: JpaRepository provides built-in CRUD methods.

Step 5: Create Service Class

Inside the service package create one class named as PersonService .

Note: Constructor-based dependency injection is preferred over @Autowired.

Step 6: Create Controller Class

Inside the controller package create one class named as PersonController.

Step 7: Configure application.properties

Below is the code for the application.properties file

Now your sample spring boot project is ready and we are going to perform unit testing in this sample project.

Step 8: Prepare Test Packages

Create the following packages and the classes as shown in the below image. (Inside the green color box)

👁 unit1

Step 9: Unit Test for Repository

Inside the test -> repo package creates one class named as PersonRepoTest .

Step 10: Unit Test for Service

Inside the test -> services package creates one class named as PersonServiceTest .

Similarly, we can perform testing of different units of your spring boot project.

Step 11: Run the Application

If we run that code on a server using port number 8082, we will get the following output.

  • API Response: List of persons in JSON format.
  • Unit Tests: Green checks for successful repository and service tests.

Test in the browser:

http://localhost:8082/persons

👁 img
output
👁 out1
Output
Comment

Explore