How to turn off output from shutdown hooks in gradle boot tests?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If Spring Boot tests are printing noisy shutdown-hook output at JVM exit, the usual cause is that the application was started in a way that registered a JVM shutdown hook and left cleanup to process termination. The clean fix is not to hide the logs first. It is to avoid relying on the shutdown hook during tests and close the application context explicitly.
Why The Output Appears
Spring Boot can register a shutdown hook so the application context closes when the JVM exits. That is useful in a real application process, but in tests it often creates extra log lines when Gradle tears down the test JVM.
A common manual startup pattern looks like this:
If the test starts an application this way and never closes the context, the shutdown hook does the cleanup later, and that cleanup can write to the test logs.
Disable The Shutdown Hook For Test Startup
If you are creating the application manually in a test, disable the hook and close the context yourself.
This is usually the most direct fix. The context is shut down under test control, so there is no delayed shutdown-hook activity when Gradle exits the JVM.
The same idea works with SpringApplicationBuilder.
Prefer Test Framework Lifecycle Over JVM Exit
If you are already using @SpringBootTest, Spring's test support usually manages application context lifecycle for you. In that setup, extra shutdown-hook output often points to code that starts additional contexts manually or spawns background resources that are not closed during the test.
A better test design is:
- let the test framework create the context
- avoid calling
SpringApplication.run(...)inside test methods - close any extra resources explicitly in test teardown
The less your test depends on JVM shutdown, the cleaner and more predictable Gradle output becomes.
Logging Is A Separate Concern
If the shutdown itself is correct but the logs are too verbose, adjust logging levels rather than redirecting System.out blindly.
For example, in src/test/resources/application.properties:
This can reduce noise, but it should not be the first fix if a shutdown hook is firing only because the test never closed a context properly.
Gradle test logging can also be narrowed.
That hides standard output streams, but again, it is better to fix the lifecycle first.
Common Pitfalls
A common mistake is trying to suppress the log output without addressing why the shutdown hook is running in the test JVM. If the application context is being left open, the shutdown hook is only a symptom.
Another mistake is calling SpringApplication.run(...) directly inside tests that already use Spring test support. That creates overlapping lifecycle management and often leaves extra contexts around.
Developers also sometimes redirect System.out and System.err globally, which can hide legitimate test diagnostics along with the unwanted output.
Finally, do not disable the shutdown hook in production code just because the test logs are noisy. This is usually a test-lifecycle problem, not an application-runtime problem.
Summary
- Shutdown-hook output in Gradle tests usually means cleanup is happening at JVM exit.
- In manually started test applications, call
setRegisterShutdownHook(false)and close the context explicitly. - Prefer Spring test lifecycle management over manual
SpringApplication.run(...)in tests. - Use logging configuration to reduce noise only after lifecycle is correct.
- Avoid global stdout redirection unless there is no cleaner option.
- Fix the cause of the hook activity instead of only hiding its output.
Related reading
- How to turn off the Eclipse code formatter for certain sections of Java code?
- How to understand when shedlock was acquired and released?
- How to unit test a Spring Boot MongoRepository?
- How to update a value, given a key in a hashmap?
- How to update Gradle in Android Studio?
- How to update Truststore dynamically?
- How to upload and retrieve file in mongodb in spring boot application without using GridFSTemplate?
- How to use 2 or more databases with spring?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.