Quick revision
The short answer
First we make the hang fail fast: a global JUnit default timeout, a fork or task timeout in the build, and a CI job timeout, so a stuck test ends in minutes with a failure instead of hours. Then we capture where it waits, with thread dumps taken at the moment of the timeout, and look for the usual causes: an await or get() without a timeout, a container that never becomes ready, a deadlock or a network call with no read timeout. We replace unbounded waits with Awaitility and explicit limits, and quarantine the test with a ticket until the fix is proven.
What to remember
In this answer, we turned an invisible weekly hang into a fast, informative failure. Timeouts at the JUnit, build-tool and CI levels make the hang end in minutes, thread dumps captured at the moment of the timeout show where the thread waits, and the dump points to the usual causes: latches and futures without limits, containers that never become ready, deadlocks and network calls without read timeouts. We replaced unbounded waits and sleeps with Awaitility and explicit limits, and kept the test in quarantine with a ticket until repeated runs proved the fix.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we make the hang fail fast: a global JUnit default timeout, a fork or task timeout in the build, and a CI job timeout, so a stuck test ends in minutes with a failure instead of hours. Then we capture where it waits, with thread dumps taken at the moment of the timeout, and look for the usual causes: an await or get() without a timeout, a container that never becomes ready, a deadlock or a network call with no read timeout. We replace unbounded waits with Awaitility and explicit limits, and quarantine the test with a ticket until the fix is proven.
Why this question is asked
A test that hangs once a week is worse than one that fails every day. It cannot be reproduced on demand, it blocks everyone's pipeline for hours, and when the job is finally killed there is nothing in the logs. Interviewers ask this because it shows whether we can turn an invisible problem into a visible one before trying to fix it. In this answer, we will add timeouts at three levels so the hang fails fast, collect thread dumps at the moment of failure, read them to find the usual causes, fix the waits with explicit limits, and handle the test while the fix is in progress.
1Why the Logs Show Nothing
A hang produces no exception, so nothing is logged. The test thread is simply parked, waiting for something that never happens. When the CI system kills the job after its global limit, often several hours, it kills the whole JVM, and the evidence of where the thread was waiting disappears with it.
So the plan has two parts. First, make the hang end on our terms, quickly, with a failure. Second, make sure that at the moment it ends, we capture the stack of every thread, because that stack is the only clue.
2Timeouts at Three Levels
Each level protects against a failure of the level below. A JUnit default timeout turns a stuck test into a failed test with the test's name. A build-level timeout kills the forked test JVM if JUnit itself cannot stop it, for example when the code ignores interrupts. A CI job timeout is the last line of defence and should be set close to the normal run time plus a margin, not to the platform's maximum.
- CI job timeout40 minutes: the whole job is cancelled
- Build tool fork or task timeout30 minutes: the test JVM is killed
- JUnit default timeout2 minutes: the single test fails by name
3 layers, from the top down. Step through them one at a time.
The innermost layer gives the most precise failure; the outer layers catch what it misses.
Notice the numbers. The integration stage normally takes 20 minutes, so the job limit is 40, not 360. A hang now costs at most 40 minutes, and usually only the two minutes of the JUnit timeout.
3Capture Thread Dumps at the Moment of Failure
A thread dump lists every thread in the JVM with its current stack and the lock or condition it waits on. It is exactly what we need. Recent JUnit versions can print a thread dump when a timeout fires, through the configuration parameter junit.jupiter.execution.timeout.threaddump.enabled (check your version). On older versions we can do the same with a small watchdog: a script in the CI job that, shortly before the job limit, runs jcmd or jstack against every Java process and saves the output as a build artifact.
The watchdog fires at 25 minutes, before the 30-minute fork timeout kills the JVM. Keeping the dumps as artifacts means the next hang, even if it happens at night, leaves a record.
4Read the Dump and Find the Cause
In the dump we look for the test worker thread and read its stack from the top. The top frames say what it is waiting on, and a few frames down we find our own code that started the wait.
| Top of the stack | Likely cause | Fix |
|---|---|---|
CountDownLatch.await() | event never arrives | await(10, SECONDS) or Awaitility |
CompletableFuture.get() | async work never completes | get(10, SECONDS), orTimeout() |
| Testcontainers wait strategy | container never ready | withStartupTimeout, check logs |
| Two threads BLOCKED on each other | deadlock | fix the lock order |
SocketInputStream read, RUNNABLE | no read timeout | set connect and read timeouts |
Here the test waits on a CountDownLatch with no time limit, probably for a Kafka listener that should count down when it receives an event. If the event is lost or arrives before the listener subscribes, the latch is never released. That is the most common pattern, but the dump points to others too. A future.get() without a timeout looks almost the same. A container that never becomes ready shows the thread inside the Testcontainers wait strategy. A deadlock appears as two threads each BLOCKED on a lock the other holds, and jstack reports it explicitly at the end with 'Found one Java-level deadlock'. A network call without a read timeout shows a thread in a socket read, RUNNABLE but never progressing.
5Replace Unbounded Waits With Explicit Ones
Once we know the cause, the fix is to make every wait finite and every sleep unnecessary. Awaitility is a small library built for exactly this: it polls a condition until it is true or a time limit passes, and on failure it says what it was waiting for.
Notice that the sleep disappears too. A fixed sleep is either too short, which causes flaky failures, or too long, which wastes time on every run. Polling with a limit is both faster and more reliable. The same rule applies to production code that the test calls: HTTP clients and database drivers need connect and read timeouts, because the test may be exposing a real bug, a call that could hang in production too.
6Quarantine Until the Fix Is Proven
A once-a-week failure cannot be confirmed fixed by one green run. While we work on it, we mark the test as flaky with a tag and a ticket and move it to a quarantine job that runs but does not block merges. Running it there, or locally with @RepeatedTest, hundreds of times gives confidence that the fix works. When it has passed consistently for an agreed time, for example two weeks, it goes back into the blocking suite.
7Follow-up questions
Why not just set a very short CI job timeout?
A job timeout kills everything and gives no detail about which test hung or why. It is the last line of defence. The JUnit timeout names the test and, with thread dumps enabled, shows where it waited, which is the information we actually need.
The dump shows the thread waiting in a socket read. Is the bug in the test?
Possibly not. It may be that the production code uses an HTTP or database client without a read timeout, and the test found a case where the other side never answers. That would hang in production too, so the fix belongs in the client configuration, and the test just made it visible.
8Conclusion
In this answer, we turned an invisible weekly hang into a fast, informative failure. Timeouts at the JUnit, build-tool and CI levels make the hang end in minutes, thread dumps captured at the moment of the timeout show where the thread waits, and the dump points to the usual causes: latches and futures without limits, containers that never become ready, deadlocks and network calls without read timeouts. We replaced unbounded waits and sleeps with Awaitility and explicit limits, and kept the test in quarantine with a ticket until repeated runs proved the fix.
Open with
The first thing is to make the hang fail fast instead of blocking for hours. I would set a JUnit default timeout in junit-platform.properties, say two minutes for test methods and more for container startup, add a fork or task timeout in Maven or Gradle in case the code ignores interrupts, and bring the CI job timeout down to around twice the normal run time.
Then, if they want more
Second, I need evidence, because a hang logs nothing. Newer JUnit versions can print a thread dump when a timeout fires; otherwise I run a small watchdog in the CI job that calls jcmd Thread.print on the test JVMs before they are killed and saves the output as an artifact. Third, I read the dump.
The test worker's stack usually shows the cause: a CountDownLatch await or a future get with no timeout, a Testcontainers wait strategy for a container that never became ready, two threads blocked on each other in a deadlock, or a socket read with no read timeout.
Then I fix the waits: every await gets a limit, sleeps are replaced by Awaitility polling with atMost, and production clients get connect and read timeouts. Finally, I tag the test as flaky with a ticket and run it in a non-blocking quarantine job, and only move it back once it has passed many repeated runs.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An integration test hangs forever in CI about once a week, blocking the pipeline for...": https://scrivio-interview-library.netlify.app/java/junit/fbd7d39b6e/