Hands‑On Unit Testing of Spring Boot Actuator Health Checks
This guide walks through setting up Spring Boot 3.x with JDK 17+, adding Actuator and test dependencies, and implementing a comprehensive JUnit 5 test suite that validates health endpoint aggregation, status priority, show‑details configuration, custom endpoints, JVM metrics, endpoint exposure, health groups, and custom HTTP status mapping, with instructions for running the tests via Maven or an IDE.
Version and requirements : The examples target Spring Boot 3.x, Spring Framework 6.x, and JDK 17+. Tests use JUnit 5 and Spring Boot Test.
Maven dependencies (pom.xml) include spring-boot-starter, spring-boot-starter-web, spring-boot-starter-actuator, spring-boot-starter-test (scope test), and spring-boot-configuration-processor (optional).
Test class – ActuatorTest
Configuration imports only the necessary Actuator auto‑configurations and the custom test components. healthEndpointAggregatesMultipleIndicators verifies that DiskSpaceHealthIndicator registers and its Health contains totalSpace, usableSpace and threshold. redisHealthIndicatorRegistered checks that RedisHealthIndicator is present and its health details include a server entry. healthEndpointAggregatesAllIndicators asserts that at least two HealthIndicator beans (disk and redis) are aggregated.
Tests for status aggregation confirm that a single DOWN indicator forces the overall status to DOWN, while all UP indicators yield UP, and that OUT_OF_SERVICE outranks UP. healthShowDetailsConfiguration demonstrates that the details map always exists in the Health object, even when show‑details=never hides them from the HTTP response. healthWithDetailChaining validates the fluent withDetail API.
Custom endpoint test customEndpointReadOperation confirms that AppInfoEndpoint registers and returns expected fields (application, version, javaVersion, os, uptime, etc.). customTestEndpoint checks that the TestMetricsEndpoint provides JVM memory, thread count, and uptime metrics.
JVM memory test jvmMemoryMetrics reads total, free, max memory and uptime via RuntimeMXBean and asserts they are positive.
JVM thread test jvmThreadMetrics verifies active and daemon thread counts and that no deadlocked threads are found.
Endpoint exposure test endpointExposureInclude ensures custom endpoints are exposed and health indicators are registered.
Endpoint ID mapping test endpointIdPathMapping confirms that @Endpoint(id="appinfo") maps to /actuator/appinfo.
Readiness vs. Liveness tests validate that readiness includes dependency checks (disk, redis) while liveness only checks the application itself.
Default HealthStatusHttpMapper mapping test checks that UP → 200 and other statuses → 503.
Custom mapper test shows that a user‑defined mapper can return 200 for UNKNOWN or custom statuses.
Component‑level HTTP mapping test builds a map from Status to HTTP code using the default mapper.
Additional tests verify custom status handling, health‑indicator naming conventions, and that DiskSpaceHealthIndicator only reports UP or DOWN.
Auxiliary classes AppInfoEndpoint: a custom Actuator endpoint exposing application name, version, start time, uptime, Java version, OS, processor count, and memory statistics. DiskSpaceHealthIndicator: checks root filesystem usable space against a 100 MB threshold and reports UP or DOWN with details. RedisHealthIndicator: attempts a TCP connection to localhost:6379; on success returns UP with server info, on failure returns DOWN with error message.
Test configuration beans provide the above indicators and endpoints for the test context.
Configuration
# Spring Boot startup demo
server.port=8080
spring.application.name=springboot-startup-demo
# Actuator endpoint exposure
management.endpoints.web.exposure.include=health,info,beans,envRunning the tests
Via Maven: mvn test -Dtest=ActuatorTest Via IntelliJ IDEA: open a test class, right‑click the class or method, and choose “Run” or “Debug”.
Related documentation : "Spring Boot Actuator and Health Checks" article.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
CodeSmart Hoops
A working programmer who loves coding and basketball. By day I debug code; by night I dissect tactics. I write articles to document my journey, focusing on Java, AI, Python and other programming topics, with occasional posts about basketball, English, and books. Hope it's helpful—thanks for following and support.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
