Best of BaeldungFebruary 2026

  1. 1
    Article
    Avatar of baeldungBaeldung·17w

    Why We Should Not Mock Collections With Mockito

    Mocking Java collections like List, Set, or Map with Mockito is an anti-pattern that leads to brittle tests and unrealistic behavior. Collections are deterministic data structures, not external dependencies requiring isolation. On Java 21+, mocking collections may fail due to stricter JVM instrumentation rules. Instead of mocking, use real collection instances in tests to create clearer, more maintainable tests that focus on observable behavior rather than implementation details. This approach exposes design issues and encourages better separation of concerns.

  2. 2
    Article
    Avatar of baeldungBaeldung·13w

    Convert an InputStream to a DataHandler in Java

    When retrieving large files from a database in Java, loading the entire content as a byte array causes OutOfMemoryError. The solution is to implement a custom DataSource that wraps an InputStream, then pass it to a DataHandler constructor. This avoids loading all data into memory at once. The tutorial walks through creating an InputStreamDataSource class implementing the DataSource interface, using it to build a DataHandler, and optionally supporting dynamic MIME type specification. Note that since Java 11, the Java Activation Framework (javax.activation) must be added as an explicit dependency.

  3. 3
    Article
    Avatar of baeldungBaeldung·17w

    Set Datasource When Creating Hibernate SessionFactory in Java

    Hibernate's SessionFactory can be explicitly configured with a Spring-managed Datasource by disabling JPA auto-configuration and manually wiring the connection pool. This approach provides fine-grained control over Hibernate bootstrapping, useful for multi-database setups or custom ORM behavior. The tutorial demonstrates using StandardServiceRegistryBuilder to inject the Datasource, configuring Hibernate properties programmatically, and validating the setup with JUnit tests in a Spring Boot application.