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

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

Unit Testing with JUnit and Mockito

Updated 2026-05-15
10 min read

Unit Testing with JUnit and Mockito

Introduction

Unit testing is a fundamental part of software development, ensuring that individual units of code work as expected. In the context of Spring Boot applications, JUnit and Mockito are popular tools for writing unit tests. This tutorial will guide you through the process of setting up and using JUnit and Mockito to write effective unit tests for your components.

Concept

What is JUnit?

JUnit is a widely-used testing framework for Java applications. It provides a simple way to write repeatable tests that can be run automatically, helping developers catch bugs early in the development cycle.

What is Mockito?

Mockito is a mocking framework for Java that allows you to create mock objects and define their behavior. This is particularly useful when your code under test depends on other components or services that are difficult to instantiate or control directly.

Examples

Let's walk through an example of how to use JUnit and Mockito to write unit tests for a Spring Boot application.

Step 1: Set Up Your Project

First, ensure you have the necessary dependencies in your pom.xml file if you're using Maven. You need JUnit, Mockito, and Spring Boot Test.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        &lt;scope&gt;test</scope>
    </dependency>
    <!-- Add other dependencies as needed -->
</dependencies>

Step 2: Create a Simple Service

Let's create a simple service that we want to test.

package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class GreetingService {

    public String greet(String name) {
        return "Hello, " + name + "!";
    }
}

### Step 3: Write Unit Tests Using JUnit and Mockito

Now, let's write a unit test for the `GreetingService` using JUnit and Mockito.

```java
package com.example.demo.service;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class GreetingServiceTest {

    @Test
    public void testGreet() {
        // Create an instance of the service
        GreetingService greetingService = new GreetingService();

        // Call the method under test
        String result = greetingService.greet("World");

        // Assert that the result is as expected
        assertEquals("Hello, World!", result);
    }
}

### Step 4: Using Mockito for Dependency Injection

Let's assume `GreetingService` depends on another service, and we want to mock that dependency.

```java
package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class GreetingService {

    private final AnotherService anotherService;

    public GreetingService(AnotherService anotherService) {
        this.anotherService = anotherService;
    }

    public String greet(String name) {
        return "Hello, " + anotherService.processName(name) + "!";
    }
}

Now, let's write a test that uses Mockito to mock `AnotherService`.

```java
package com.example.demo.service;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class GreetingServiceTest {

    @Test
    public void testGreetWithMock() {
        // Create a mock instance of AnotherService
        AnotherService anotherService = mock(AnotherService.class);

        // Define the behavior of the mock
        when(anotherService.processName("World")).thenReturn("Mocked World");

        // Create an instance of GreetingService with the mocked dependency
        GreetingService greetingService = new GreetingService(anotherService);

        // Call the method under test
        String result = greetingService.greet("World");

        // Assert that the result is as expected
        assertEquals("Hello, Mocked World!", result);
    }
}

What's Next?

In this tutorial, we covered how to write unit tests for your components using JUnit and Mockito. The next step in testing Spring Boot applications is integration testing, which involves testing multiple components working together. You can explore "Integration Testing with Spring Boot Test" to learn more about this topic.

By mastering unit testing, you'll be able to ensure the reliability of your code and catch issues early in the development process. Happy coding!


PreviousTesting Basics in Spring BootNext Integration Testing with Spring Boot Test

Recommended Gear

Testing Basics in Spring BootIntegration Testing with Spring Boot Test