Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:
How to Unit Test Micrometer
Last updated: October 3, 2025
1. Overview
In this article, we’ll explore how to unit test Micrometer metrics in a Spring Boot application. We’ll focus on testing meters programmatically using a simple meter registry.
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’ll learn how to reset the MeterRegistry to perform assertions on it during our @SpringBootTests.
2. Testing Micrometer
Let’s start with a simple service that records metrics using Micrometer. Our FooService uses a MeterRegistry to track method invocations with a counter and execution time with a timer:
@Service
class FooService {
// constructor
private final MeterRegistry registry;
public int foo() {
int delayedMs = registry.timer("foo.time")
.record(this::doSomething);
registry.counter("foo.count")
.increment();
return delayedMs;
}
private int doSomething() { /* ... */ }
}
2.1. Unit Tests
For unit testing, we can create a SimpleMeterRegistry programmatically and inject it into our service. This approach allows us to test metric collection in isolation without needing Spring Boot’s auto-configuration:
class MicrometerUnitTest {
MeterRegistry meterRegistry = new SimpleMeterRegistry();
FooService fooService = new FooService(meterRegistry);
@Test
void whenFooIsCalled_thenCounterIsIncremented() {
fooService.foo();
fooService.foo();
fooService.foo();
double invocations = meterRegistry.get("foo.count")
.counter()
.count();
assertThat(invocations)
.isEqualTo(3);
}
}
As we can see, we use the meterRegistry.get() method to retrieve meters by name and then access their recorded values. For counters, we call count() to get the total number of increments. Similarly, for timers or other types of meters, we can use the meterRegistry to find the meter instance and perform the relevant assertions.
The SimpleMeterRegistry stores all metrics in memory, making it perfect for unit tests where we want to verify that our application correctly records metrics without the overhead of a full monitoring system setup.
2.2. Integration Tests
If we want to test these meters during a @SpringBootTest, we might notice different behavior, especially when multiple tests call our tested component:
@SpringBootTest(classes=FooApplication.class)
class MicrometerIntegrationTest {
@Autowired
private MeterRegistry meterRegistry;
@Autowired
private FooService fooService;
@Test
void whenFooIsCalled_thenCounterIsIncremented() {
fooService.foo();
fooService.foo();
fooService.foo();
double invocations = meterRegistry.get("foo.count")
.counter()
.count();
assertThat(invocations)
.isEqualTo(3); // <-- this can fail, depending on the order of the test execution
}
@Test
void whenFooIsCalled_thenTimerIsUpdated() {
// ...
}
}
In this case, the meter registry is shared across all test methods. Consequently, the meters accumulate values from previous test executions, which could cause our assertions to fail.
To fix this, we can clear the meterRegistry before or after each test:
@SpringBootTest(classes = FooApplication.class )
class MicrometerIntegrationTest {
@Autowired
private MeterRegistry meterRegistry;
@Autowired
private FooService fooService;
@BeforeEach
void reset() {
meterRegistry.clear();
}
@Test
void whenFooIsCalled_thenCounterIsIncremented() {
// ...
}
@Test
void whenFooIsCalled_thenTimerIsUpdated() {
// ...
}
}
3. The Micrometer Test Module
For simple assertions, we could even import the dedicated micrometer-test module:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-test</artifactId>
<version>1.15.3</version>
<scope>test</scope>
</dependency>
This will allow us to use a set of AssertJ-like assertions to verify that a specific meter was registered:
@Test
void whenFooIsCalled_thenTimerIsRegistered() {
fooService.foo();
MeterRegistryAssert.assertThat(meterRegistry)
.hasTimerWithName("foo.time");
}
As we can see, Micrometer’s test-specific module enables us to write elegant assertions, though only for checking the existence of metrics. We’ll need to write our own assertions for the rest, such as checking the exact values recorded by the meters.
4. Conclusion
In this short tutorial, we demonstrated how to unit test Micrometer metrics using a SimpleMeterRegistry. We discussed testing various meters by using the meterRegistry to programmatically fetch the meters and perform the assertions on the collected values.
After that, we learned how to perform assertions on meters during integration tests. As we saw, the testing approach was quite similar, the only notable difference being that with SpringBootTest, we need to clear the MeterRegistry before each test.
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.















