Unit testing metrics is crucial for ensuring that our application correctly tracks performance data and business metrics. By using Micrometer’s SimpleMeterRegistry, we can create isolated tests that verify our metrics collection without requiring a full Spring context. When it comes to integration tests, we need to reset the MeterRegistry to perform assertions on it during our @SpringBootTests.
Also find me here:
Baeldung Editor
Josh Cummings
Josh has been a software engineer for over 15 years building enterprise applications across multiple industries. He has long been passionate about application security and loves opportunities to mentor and to learn from others. When Josh isn't hacking away at code, he is either running, playing basketball, camping, or reading a Brandon Sanderson novel.
Here's what I've reviewed (so far):
Baeldung on Java
- All
- Spring (11)
- Java (11)
- Testing (10)
- Core Java (10)
- Persistence (9)
- Spring Boot (8)
- JPA (8)
- Data (8)
- Security (7)
- Java String (7)
- DevOps (7)
- Algorithms (7)
- JSON (5)
- Architecture (5)
- Spring Persistence (4)
- Reactive (4)
- Maven (4)
- Java IO (4)
- IDE (4)
- Spring Security (3)
- Spring MVC (3)
- Spring Cloud (3)
- Logging (3)
- Java Array (3)
- Jakarta EE (3)
- Jackson (3)
- Web Services (2)
- Spring Data (2)
- REST (2)
- Java Map (2)
- Java Dates (2)
- HTTP Client-Side (2)
- Groovy (2)
- Artificial Intelligence (2)
- XML (1)
- Spring AI (1)
- NoSQL (1)
- Java Streams (1)
- Java List (1)
- Java Concurrency (1)
- Java Collections (1)
- JVM (1)
- Gradle (1)
- Docker (1)
- Cloud (1)
Preventing Jackson From Fetching Lazy Entity Fields
Working with JPA and Jackson often raises the challenge of serializing entities that contain lazy-loaded associations. This tutorial shows how to prevent Jackson from triggering unwanted database queries when lazy fields are uninitialized; we’ll look at DTOs, annotations, and Jackson’s own jackson-datatype-hibernate6 module.
Stable Values in Java 25
Filed under Core Java
Explore the preview feature Stable Value API (JEP 502) introduced in Java 25.
TupleTransformer and ResultListTransformer in Hibernate
Filed under Persistence
While JPQL projections solve many cases, sometimes we need more flexibility, like mapping complex DTOs, restructuring query results, or post-processing the entire result set. Hibernate’s former answer for this is ResultTransformer. Hibernate 6, however, replaces it with two new interfaces. This article shows how to work with TupleTransformer for row-level transformations and ResultListTransformer for list-level processing.
Tagging Patterns for Micrometer Metrics
In this article, we explore Micrometer metrics with a focus on tagging. We use Micrometer in a Spring Boot application and apply various patterns to create simple metrics, such as counters and timers.
We’ll start by using Micrometer’s Builder API to create meters with variable tag values. Additionally, we’ll look at MeterProviders as an alternative that allows us to avoid potential performance issues. We’ll also use Spring AOP and Micrometer-specific annotations to record method invocations in a declarative manner.
Storing X and Y Coordinates in Java
Filed under Core Java
Explore options for storing coordinates in Java: records, POJOs, arrays, and the tradeoffs of each.
Using HTTPS With Jersey Client in Java
Filed under HTTP Client-Side, Security
Configuring Java clients for HTTPS or for mTLS can be tricky. It’s tempting to add some kind of trust-all-certs solution and call it good. Fortunately there are Java libraries like Jersey that can help us make the right decisions.
Implementing CQRS with Spring Modulith
Filed under Architecture, Spring
Let’s take a fresh look at the CQRS pattern, exploring its benefits and trade-offs in a modular Spring Boot application. We’ll use Spring Modulith to structure our code into clearly separated modules and to enable asynchronous, event-driven communication between them.
This approach is inspired by our colleague Gaetano Piazzolla’s article, where he demonstrates CQRS with Spring Modulith in a product catalog. Here, we’ll adapt the same ideas to a movie ticket booking system and keep the two sides in sync via domain events.
Fixing HibernateException: Illegal Attempt to Associate a Collection With Two Open Sessions
Filed under JPA
Examine the root cause of the Hibernate Exception: “Illegal attempt to associate a collection with two open sessions.” and how to fix it.