codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

Spring Boot

40 / 62 topics
40Testing Basics in Spring Boot41Unit Testing with JUnit and Mockito42Integration Testing with Spring Boot Test43Performance Testing with JMeter
Tutorials/Spring Boot/Testing Basics in Spring Boot
🍃Spring Boot

Testing Basics in Spring Boot

Updated 2026-05-15
10 min read

Testing Basics in Spring Boot

Introduction

Welcome to the section on testing basics in Spring Boot! Testing is a crucial part of software development, ensuring that your application behaves as expected and helps identify issues early in the development cycle. Spring Boot provides robust support for various types of tests, including unit tests, integration tests, and end-to-end tests.

In this tutorial, we'll cover the fundamentals of testing in Spring Boot applications. We'll start by understanding the different types of tests and then dive into practical examples using JUnit and Mockito.

Concept

Types of Tests

  1. Unit Tests: These tests focus on individual components or methods within your application. They are typically fast and isolated, meaning they do not rely on external systems like databases or web services.

  2. Integration Tests: These tests check the interaction between different parts of your application. They might involve database access, REST API calls, or other external dependencies.

  3. End-to-End (E2E) Tests: These tests simulate user interactions with your application from start to finish. They are often used to test the entire flow of an application.

Testing Frameworks

Spring Boot leverages popular testing frameworks like JUnit and Mockito:

  • JUnit: A widely-used Java testing framework that provides a simple way to write repeatable tests.
  • Mockito: A mocking framework for Java that allows you to create mock objects, which are used to simulate the behavior of real objects in your tests.

Examples

Setting Up Your Project

Before we dive into writing tests, ensure your Spring Boot project is set up correctly. If you haven't already created a Spring Boot application, you can use Spring Initializr to generate one.

Once your project is ready, add the following dependencies to your pom.xml if you're using Maven:

XML
1<dependencies>
2 <dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter-test</artifactId>
5 <scope>test</scope>
6 </dependency>
7</dependencies>

Writing a Simple Unit Test

Let's start with a simple unit test for a service class. Assume you have the following GreetingService class:

Java
1package com.example.demo;
2
3public class GreetingService {
4 public String greet(String name) {
5 return "Hello, " + name + "!";
6 }
7}

Now, let's write a unit test for this service using JUnit and Mockito.

Java
1package com.example.demo;
2
3import org.junit.jupiter.api.Test;
4import static org.junit.jupiter.api.Assertions.assertEquals;
5
6public class GreetingServiceTest {
7
8 @Test
9 public void testGreet() {
10 GreetingService greetingService = new GreetingService();
11 String result = greetingService.greet("World");
12 assertEquals("Hello, World!", result);
13 }
14}

Writing an Integration Test

For integration tests, we might want to check how our service interacts with a repository. Let's assume you have a UserRepository interface and a corresponding UserService.

Java
1package com.example.demo;
2
3import org.springframework.data.jpa.repository.JpaRepository;
4import org.springframework.stereotype.Repository;
5
6@Repository
7public interface UserRepository extends JpaRepository<User, Long> {
8}
Java
1package com.example.demo;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.stereotype.Service;
5
6@Service
7public class UserService {
8
9 @Autowired
10 private UserRepository userRepository;
11
12 public User getUserById(Long id) {
13 return userRepository.findById(id).orElse(null);
14 }
15}

Now, let's write an integration test for the UserService.

Java
1package com.example.demo;
2
3import org.junit.jupiter.api.Test;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.boot.test.context.SpringBootTest;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7
8@SpringBootTest
9public class UserServiceTest {
10
11 @Autowired
12 private UserService userService;
13
14 @Test
15 public void testGetUserById() {
16 User user = userService.getUserById(1L);
17 assertNotNull(user);
18 }
19}

Using Mockito for Mocking

Mockito is particularly useful when you want to isolate your tests from external dependencies. Let's modify the UserService test to use Mockito to mock the UserRepository.

Java
1package com.example.demo;
2
3import org.junit.jupiter.api.Test;
4import org.mockito.InjectMocks;
5import org.mockito.Mock;
6import org.springframework.boot.test.context.SpringBootTest;
7import static org.junit.jupiter.api.Assertions.assertNotNull;
8import static org.mockito.Mockito.when;
9
10@SpringBootTest
11public class UserServiceTest {
12
13 @Mock
14 private UserRepository userRepository;
15
16 @InjectMocks
17 private UserService userService;
18
19 @Test
20 public void testGetUserById() {
21 User user = new User();
22 when(userRepository.findById(1L)).thenReturn(java.util.Optional.of(user));
23 assertNotNull(userService.getUserById(1L));
24 }
25}

What's Next?

In the next section, we'll explore unit testing in more detail using JUnit and Mockito. We'll cover advanced topics like mocking dependencies, parameterized tests, and test-driven development (TDD).

Stay tuned for more insights into building robust and maintainable Spring Boot applications!


PreviousCircuit Breaker Pattern with HystrixNext Unit Testing with JUnit and Mockito

Recommended Gear

Circuit Breaker Pattern with HystrixUnit Testing with JUnit and Mockito