How to test methods that call System.exit?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Testing code that calls System.exit() is awkward because a real call terminates the JVM and kills the test process. The cleanest solution is usually not to intercept System.exit() directly, but to refactor the code so the exit decision is testable and the actual JVM shutdown happens only at the outermost application boundary.
You can still trap exit attempts in tests with helper libraries, but that should be a fallback. If your business logic depends on hard process termination, the design is usually fighting your testability for no good reason.
Prefer Refactoring Over Interception
Instead of calling System.exit() deep inside your logic, delegate the termination step to an interface.
Then the application logic depends on ExitHandler, not on the JVM directly.
The test becomes straightforward.
This is usually the best answer because it tests the behavior you actually care about without threatening the test process.
If You Must Intercept System.exit()
Sometimes you are testing legacy code that you cannot refactor immediately. In that case, use a test helper library that traps exit attempts.
A common modern option is System Lambda.
That lets you verify the exit code without actually terminating the JVM running the tests.
Why SecurityManager-Based Tests Are a Legacy Path
Older answers often recommend installing a custom SecurityManager and throwing an exception from checkExit. That used to be a common workaround, but it is no longer the best modern guidance.
The reason is simple: it couples tests to a legacy JVM mechanism and keeps the production design centered around global process termination instead of testable control flow.
If you are maintaining old code, you may still encounter that pattern, but prefer refactoring or a dedicated testing library when possible.
Keep Process Exit at the Edge
A useful design rule is that only the outermost CLI launcher should call System.exit(). The inner application should return an exit code or throw an exception.
Now runApplication is trivial to unit test, and only the tiny main wrapper remains tied to actual JVM termination.
Common Pitfalls
A common mistake is calling System.exit() from deep inside reusable service code. That makes the code hostile to testing and awkward to reuse outside a command-line entry point.
Another issue is trying to unit test hard process termination without separating business logic from the final launcher behavior. The test becomes about JVM shutdown mechanics instead of application behavior.
Developers also sometimes rely on old SecurityManager-based techniques without noticing that the better design is to avoid the dependency entirely.
Finally, if you use an exit-trapping library, keep the tests focused on exit status and conditions. Do not let it become a substitute for refactoring obviously test-hostile code.
Summary
- The best way to test
System.exit()logic is usually to refactor the exit behavior behind an interface or return code. - Keep actual JVM termination at the application boundary, not in core business logic.
- For legacy code, use a helper such as System Lambda to trap exit attempts in tests.
- Avoid leaning on old SecurityManager-based interception as the default solution.
- Test the decision to exit, not just the low-level shutdown side effect.
Related reading
- How to test Spring Scheduled
- How to train image pixel data in libsvm format to use for recognition with Java
- How to trigger a scheduled Spring Batch Job?
- How to turn off debug log messages in spring boot
- How to test multiple variables for equality against a single value?
- How to test multiple variables for equality against a single value?
- How to test that no exception is thrown?
- How to test the connection to RabbitMQ Server?

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.