Featured Article

JUnit Mockito Tutorial Spring Boot: Master Testing with Easy Examples

Kenneth Jul 13, 2026

In the realm of Spring Boot development, testing is a crucial aspect that ensures the reliability and performance of your applications. Two popular tools for unit testing in Spring Boot are JUnit and Mockito. This tutorial will guide you through the process of using these tools effectively to create robust and maintainable tests in your Spring Boot projects.

Spring Boot + RESTful + JUnit + Mockito + Hamcrest + EclEmma
Spring Boot + RESTful + JUnit + Mockito + Hamcrest + EclEmma

Before diving into the details, let's briefly understand what JUnit and Mockito bring to the table. JUnit is a widely-used unit testing framework for Java applications, providing features like assertions and test fixture setup and teardown. Mockito, on the other hand, is a mocking framework that allows you to create mock objects with preconfigured behavior, making it easier to test your code in isolation.

Top 5 Udemy Courses to Learn JUnit 5, Mockito, and TDD in 2025 - Best of Lot
Top 5 Udemy Courses to Learn JUnit 5, Mockito, and TDD in 2025 - Best of Lot

Setting Up JUnit and Mockito in Spring Boot

To start using JUnit and Mockito in your Spring Boot project, you first need to include their dependencies in your build file. If you're using Maven, add the following dependencies to your pom.xml file:

the instructions for how to make an elf boots
the instructions for how to make an elf boots

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
  </dependency>
</dependencies>

The spring-boot-starter-test dependency includes JUnit and Mockito, along with other useful testing tools like AssertJ and Hamcrest.

Configuring Test Scope and Mockito Version

More useful fashion tipsπŸ‘‰πŸ» @marina_usti_  . . . . #kneehighboots #ankleboots  what type of boots make your legs look longer Boots For Short Legs, Long Boots For Short Women, Ankle Boots For Short Legs, Boots For Short Legs Best, Boot Lengths, Boot Height Guide Style, Women's Biker Boot Fashion Tips, Boot Height Guide, How To Make Your Legs Look Shorter
More useful fashion tipsπŸ‘‰πŸ» @marina_usti_ . . . . #kneehighboots #ankleboots what type of boots make your legs look longer Boots For Short Legs, Long Boots For Short Women, Ankle Boots For Short Legs, Boots For Short Legs Best, Boot Lengths, Boot Height Guide Style, Women's Biker Boot Fashion Tips, Boot Height Guide, How To Make Your Legs Look Shorter

By default, the tests will be executed using JUnit 5 (JUnit Jupiter), and Mockito 3 will be included. However, you can configure the testing scope and specify the Mockito version if needed. To use Mockito 2 with JUnit 4, you can follow the official Spring Boot documentation for configuring testing scope.

Writing Unit Tests with JUnit and Mockito

Now that we have set up the necessary dependencies, let's dive into writing unit tests using JUnit and Mockito. We'll demonstrate this with a simple Spring Boot application that has a service layer with a dependency on a repository.

Knit Pink Marshmallow Baby Booties
Knit Pink Marshmallow Baby Booties

Mocking Dependencies

In order to test the service layer in isolation, we'll use Mockito to create mock objects of the repository dependency. Here's a basic example:

```java import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import static org.mockito.Mockito.*; class MyServiceTest { @InjectMocks private MyService myService; @Mock private MyRepository myRepository; @BeforeEach void setUp() { MockitoAnnotations.initMocks(this); } @Test void testMyServiceMethod() { // Given when(myRepository.findById(1L)).thenReturn(Optional.of(new MyEntity())); // When MyEntity result = myService.myServiceMethod(1L); // Then assertNotNull(result); verify(myRepository).findById(1L); } } ```

In this example, we're using the @InjectMocks and @Mock annotations from Mockito to inject a mock repository into the service being tested. The setUp() method is annotated with @BeforeEach and is responsible for initializing the mocks using MockitoAnnotations.initMocks(this);. Inside the test method, we use Mockito's when and thenReturn to stub the behavior of the mock repository and make assertions about the result of the service method.

an image of the pattern for a pair of slippers, with instructions to make it
an image of the pattern for a pair of slippers, with instructions to make it

Using Mockito Verifications

After calling methods on the mock objects, we can use Mockito's verify feature to ensure that certain methods were called with specific arguments. In the previous example, we used verify(myRepository).findById(1L); to verify that the findById method was called exactly once with the argument 1L.

a pair of gray boots with flowers painted on them
a pair of gray boots with flowers painted on them
Crochet Boots, Free Crochet Pattern For Baby Boots, Easy Crochet Baby Bootie Pattern, Easy Crochet Baby Boots, Crochet Baby Bootie With Hook, Free Baby Boots Crochet Pattern, Baby Shoes Pattern, Free Crochet Baby Bootie Pattern, How To Crochet Baby Slippers
Crochet Boots, Free Crochet Pattern For Baby Boots, Easy Crochet Baby Bootie Pattern, Easy Crochet Baby Boots, Crochet Baby Bootie With Hook, Free Baby Boots Crochet Pattern, Baby Shoes Pattern, Free Crochet Baby Bootie Pattern, How To Crochet Baby Slippers
a drawing of a pair of shoes with laces on the bottom and soles attached to them
a drawing of a pair of shoes with laces on the bottom and soles attached to them
ζŠ˜γ‚Šη΄™ ブーツ ι•·γγ€γ‚’δ½œγ£γ¦γΏγŸ!δ½œγ‚Šζ–Ή/How to make Origami boots.rain boots. wellingtons.Handicraft.Paper craft.
ζŠ˜γ‚Šη΄™ ブーツ ι•·γγ€γ‚’δ½œγ£γ¦γΏγŸ!δ½œγ‚Šζ–Ή/How to make Origami boots.rain boots. wellingtons.Handicraft.Paper craft.
Sewing Pattern Boot Pattern Template, Diy Boots Pattern, How To Make Boot Patterns, Diy Boot Pattern, Boot Pattern Template, Diy Boot Making Tutorial, Template Doll Boot Pattern, Doll Boots Pattern How To Make, Doll Boot Pattern
Sewing Pattern Boot Pattern Template, Diy Boots Pattern, How To Make Boot Patterns, Diy Boot Pattern, Boot Pattern Template, Diy Boot Making Tutorial, Template Doll Boot Pattern, Doll Boots Pattern How To Make, Doll Boot Pattern
there are many pictures of someone's feet and boots in different stages of construction
there are many pictures of someone's feet and boots in different stages of construction
Cherokee Boots, Turtle Shell Boots, Moon Moccasins, Wabanaki Boots, Tracker Moccasins, Turtle Boot, Luxury Bridle Leather Moccasins Moc Toe, Snapping Turtle Boots, Deerskin Boots
Cherokee Boots, Turtle Shell Boots, Moon Moccasins, Wabanaki Boots, Tracker Moccasins, Turtle Boot, Luxury Bridle Leather Moccasins Moc Toe, Snapping Turtle Boots, Deerskin Boots
Barefoot Leather highboots
Barefoot Leather highboots
OH NO IT’S BEEN CHANGED
OH NO IT’S BEEN CHANGED

unganle Mockito with Spring Boot's @SpringBootTest

While the previous example demonstrates unit testing in isolation, you might also want to test components that rely on spring Context. In such cases, you can use Spring Boot's @SpringBootTest annotation to load the entire Spring Context, including your application's components, and use Mockito to stub external dependencies. Here's an example:

```java @SpringBootTest class MyIntegrationTest { @Autowired private MyService myService; @MockBean private MyRepository myRepository; @Test void testMyIntegrationTest() { // Given when(myRepository.findById(1L)).thenReturn(Optional.of(new MyEntity())); // When MyEntity result = myService.myServiceMethod(1L); // Then assertNotNull(result); verify(myRepository).findById(1L); } } ```

In this integration test, we're using the @SpringBootTest annotation to load the entire Spring Context and the @MockBean annotation to replace the real repository with a mock object. This allows us to test the integration of the service with the repository while still controlling the repository's behavior with Mockito.

In conclusion, mastering JUnit and Mockito is essential for creating effective unit tests in your Spring Boot applications. By learning how to configure these tools and leveraging their features, you can write robust and maintainable tests that ensure the reliability and performance of your code. Don't forget to explore Mockito's advanced features, such as verifications, stubbing with arguments, and custom matchers, to take your testing to the next level. Happy testing!