eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

eBook – Guide Junit – NPI (tag = JUnit)
announcement - icon

Improve your tests with JUnit 5, from mastering the basics to employing the new powerful features from JUnit 5 like extensions, tagging, filtering, parameterized tests, and more:

>> The Junit 5 handbook

Partner – Diagrid – NPI (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

In this tutorial, we’ll discuss testing code that uses JDBC objects to interact with the database. Initially, we’ll use Mockito to stub all the java.sql objects involved in acquiring a JDBC Connection, creating a Statement, executing a query and retrieving the data from the ResultSet.

After that, we’ll analyze the pros and cons of this approach and understand why nesting mocks often lead to fragile tests. Finally, we’ll explore alternatives such as opting for tests with a larger scope or refactoring our code to increase its testability.

2. Code Samples

For the code samples in this article, we’ll assume we are working on a Java class responsible for executing an SQL query, filtering the ResultSet, and mapping it to Java objects.

The class we want to test will receive a DataSource as a dependency, and it’ll use this DataSource to acquire a Connection, create a Statement, and execute a query:

class CustomersService {

    private final DataSource dataSource;

    // constructor

    public List<Customer> customersEligibleForOffers() throws SQLException {
        try (
            Connection conn = dataSource.getConnection(); 
            Statement stmt = conn.createStatement()
        ) {
            ResultSet resultSet = stmt.executeQuery("SELECT * FROM Customers");
            List<Customer> customers = new ArrayList<>();

            while (resultSet.next()) {
                Customer customer = mapCustomer(resultSet);
                // more business logic ...
                if (customer.status() == Status.ACTIVE 
                  || customer.status() == Status.LOYAL) {
                    customers.add(customer);
                }
            }
            return customers;
        }
    }

    private Customer mapCustomer(ResultSet resultSet) throws SQLException {
        return new Customer(
            resultSet.getInt("id"),
            resultSet.getString("name"),
            Status.valueOf(resultSet.getString("status"))
        );
    }
}

As we can see in the customersEligibleForOffer() method, we query the Customers table and programmatically filter out the entries with an inadequate status. Then, we leverage ResultSet’s API to get the relevant information for creating Customer objects and return a list of them.

3. Mocking java.sql Objects

To test the CustomerService class, we can try mocking the DataSource dependency. However, we’ll soon realize that a single mock object will not be enough. This happens because the DataSource initiates a Connection, which creates a Statement that produces a ResultSet containing the raw data we want to stub.

Simply put, if we want to use Mockito to stub the data from the ResultSet, we’ll need to introduce four mocks:

@ExtendWith(MockitoExtension.class)
class JdbcMockingUnitTest {

    @Mock
    DataSource dataSource;
    @Mock
    Connection conn;
    @Mock
    Statement stmt;
    @Mock
    ResultSet resultSet;

    @Test
    void whenFetchingEligibleCustomers_thenTheyHaveCorrectStatus() throws Exception {
        // ...
    }

}

Then, we’ll make sure that each mock – when invoked – returns the next one, starting from the DataSource all the way to the ResultSet:

@Test
void whenFetchingEligibleCustomers_thenTheyHaveCorrectStatus() throws Exception {
    CustomersService customersService = new CustomersService(dataSource);

    when(dataSource.getConnection())
      .thenReturn(conn);
    when(conn.createStatement())
      .thenReturn(stmt);
    when(stmt.executeQuery("SELECT * FROM customers"))
      .thenReturn(resultSet);
    
    // ...
}

After that, we’ll add the stubbing for the ResultSet itself. Let’s configure it to return three customers with different statuses:

when(resultSet.next())
  .thenReturn(true, true, true, false);
when(resultSet.getInt("id"))
  .thenReturn(1, 2, 3);
when(resultSet.getString("name"))
  .thenReturn("Alice", "Bob", "John");
when(resultSet.getString("status"))
  .thenReturn("LOYAL", "ACTIVE", "INACTIVE");

Finally, we’ll perform some assertions. In this case, we expect only Alice and Bob to be returned as customers eligible for offers.

Let’s take a look at the whole test:

@Test
void whenFetchingEligibleCustomers_thenTheyHaveCorrectStatus() throws Exception {
    //given
    CustomersService customersService = new CustomersService(dataSource);

    when(dataSource.getConnection())
      .thenReturn(conn);
    when(conn.createStatement())
      .thenReturn(stmt);
    when(stmt.executeQuery("SELECT * FROM customers"))
      .thenReturn(resultSet);

    when(resultSet.next())
      .thenReturn(true, true, true, false);
    when(resultSet.getInt("id"))
      .thenReturn(1, 2, 3);
    when(resultSet.getString("name"))
      .thenReturn("Alice", "Bob", "John");
    when(resultSet.getString("status"))
      .thenReturn("LOYAL", "ACTIVE", "INACTIVE");

    // when
    List<Customer> eligibleCustomers = customersService.customersEligibleForOffers();

    // then
    assertThat(eligibleCustomers).containsExactlyInAnyOrder(
        new Customer(1, "Alice", Status.LOYAL),
        new Customer(2, "Bob", Status.ACTIVE)
    );
}

That’s it! We can now execute the test and verify that the tested component correctly filters and maps the ResultSet data.

4. Disadvantages

Even though our solution allows testing the filtering and mapping logic, the test is verbose and fragile. While we can extract some helper methods to improve the readability, the test is tightly coupled to the implementation.

Nested mocks require a strict sequence of method invocations. This makes tests fragile, failing even when refactoring doesn’t change our function’s behavior. This fragility arises because our component is tightly coupled to the DataSource it receives as a dependency. If we refactor our code to interact differently with any of the mocked objects, the test will fail even if the underlying behavior remains unchanged.

Simply put, the mocks don’t allow us to change how we acquire the connection, the type of statement we use, or the SQL query itself – without breaking the test. The same happens if we switch to a higher-level API to interact with the database, like JdbcTemplate or JdbcClient.

Sometimes, tests that are too fragile or hard to write reveal deeper design issues. In our case, they expose the lack of a clear separation between our custom logic and the internals of our persistence layer – a violation of the Single Responsibility Principle.

5. Alternatives

We can avoid the anti-pattern of mocks returning other mocks by testing a larger scope. Instead of writing a unit test and mocking JDBC objects, we can write an integration test and embrace database interaction, leveraging tools like the Embedded H2 Database or Testcontainers.

On the other hand, we can refactor our code to separate the two distinct responsibilities. For example, we can apply the Dependency Inversion Principle and ensure that CustomerServiceV2 depends on an interface for fetching data from the database, rather than directly depending on the java.sql API. Usually, we’ll create a custom interface, but for brevity, we’ll use Java’s Supplier:

class CustomersServiceV2 {

    private final Supplier<List<Customer>> findAllCustomers;

    // constructor

    public List<Customer> customersEligibleForOffers() { 
        return findAllCustomers.get()
          .stream()
          .filter(customer -> customer.status() == Status.ACTIVE 
            || customer.status() == Status.LOYAL)
          .toList();
    }

}

Next, we’ll create a class that implements the Supplier<List<Customer>> interface and internally uses JDBC to fetch and map the data:

class AllCustomers implements Supplier<List<Customer>> {

    private final DataSource dataSource;

    // constructor

    @Override
    public List<Customer> get() {
        try (
          Connection conn = dataSource.getConnection(); 
          Statement stmt = conn.createStatement()
        ) {
            ResultSet resultSet = stmt.executeQuery("SELECT * FROM customers");
            List<Customer> customers = new ArrayList<>();
            while (resultSet.next()) {
                customers.add(mapCustomer(resultSet));
            }
            return customers;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private Customer mapCustomer(ResultSet resultSet) throws SQLException {
        // ...
    }

}

Since our domain service is decoupled from the persistence mechanism, we can freely test our business logic without creating mocks for each JDBC object. Moreover, Supplier is a functional interface, meaning we can test CustomerServiceV2 with no mocks at all! Instead, we’ll simply provide a different, in-lined test implementation of the Supplier using a lambda expression:

@Test
void whenFetchingEligibleCustomersFromV2_thenTheyHaveCorrectStatus() {
    // given
    List<Customer> allCustomers = List.of(
        new Customer(1, "Alice", Status.LOYAL),
        new Customer(2, "Bob", Status.ACTIVE),
        new Customer(3, "John", Status.INACTIVE)
    );

    CustomersServiceV2 service = new CustomersServiceV2(() -> allCustomers);

    // when
    List<Customer> eligibleCustomers = service.customersEligibleForOffers();

    // then
    assertThat(eligibleCustomers).containsExactlyInAnyOrder(
        new Customer(1, "Alice", Status.LOYAL),
        new Customer(2, "Bob", Status.ACTIVE)
    );
}

These alternatives enable us to refactor the code or change the way we fetch data from the database without affecting the test outcome. As we can see, both approaches focus more on the system behavior and result in more robust tests.

6. Conclusion

In this article, we learned how to use Mockito to test code that uses JDBC to interact with the database. We discovered that the java.sql API will require us to create multiple, nested mocks – and we covered why this might be considered a bad practice.

Following that, we refactored our code towards a more testable solution and verified it without using any mock whatsoever. Additionally, we discussed the usage of specialized tools such as Embedded H2 Database or Testcontainers that enable us to truly test the integration between our code and the database.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook – Mockito – NPI (tag=Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

>> Download the eBook

eBook Jackson – NPI EA – 3 (cat = Jackson)