DEV Community

realNameHidden
realNameHidden

Posted on

2 1 1 1 1

doNothing()method example Spring Boot

When to Use doNothing()?

When testing void methods that perform side effects (e.g., sending emails, logging, updating databases).

When you want to verify that a void method was called without actually executing it.

Spring Boot Example Using doNothing()

Employee.java (Model Class)

package com.example.demo.model;

public class Employee {
    private String id;
    private String name;

    public Employee(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

Enter fullscreen mode Exit fullscreen mode

EmployeeRepository.java (Simulating Database Calls)

package com.example.demo.repository;

import com.example.demo.model.Employee;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class EmployeeRepository {
    private final Map<String, Employee> employeeData = new HashMap<>();

    public void save(Employee employee) {
        System.out.println("Saving employee to database: " + employee.getName());
        employeeData.put(employee.getId(), employee);
    }
}

Enter fullscreen mode Exit fullscreen mode

EmployeeService.java (Business Logic)

package com.example.demo.service;

import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import org.springframework.stereotype.Service;

@Service
public class EmployeeService {
    private final EmployeeRepository employeeRepository;

    public EmployeeService(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
    }

    public void addEmployee(Employee employee) {
        employeeRepository.save(employee); // Void method that interacts with DB
    }
}

Enter fullscreen mode Exit fullscreen mode

Writing a Test Using doNothing()

Scenario:
We don’t want save() to actually modify data in the repository during the test. Instead, we mock it using doNothing().

package com.example.demo.service;

import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class EmployeeServiceTest {

    @Mock
    private EmployeeRepository employeeRepository; // Mock repository

    @InjectMocks
    private EmployeeService employeeService; // Inject mocks into service

    @Test
    void testAddEmployee_DoNothing() {
        // Arrange
        Employee employee = new Employee("1", "Mock Employee");

        // Stub the void method to do nothing
        doNothing().when(employeeRepository).save(employee);

        // Act
        employeeService.addEmployee(employee);

        // Assert: Verify the method was called once but didn’t execute
        verify(employeeRepository, times(1)).save(employee);
    }
}

Enter fullscreen mode Exit fullscreen mode

Explanation of doNothing()

Why doNothing()?

Since save() is a void method, when().thenReturn() won’t work.

doNothing().when(employeeRepository).save(employee) prevents actual execution.

Why verify()?

Ensures save(employee) was actually called once, without executing its logic.

When to Use doNothing()?

Mocking void methods to prevent unwanted side effects.

Avoiding actual database writes or external API calls.

Testing only method calls and execution flow.

Conclusion

Use doNothing() to mock void methods that should not execute real logic.

Combine it with verify() to ensure method invocation.

Helps isolate business logic from dependencies during testing.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay