I know from the tags that this question was aimed at JUnit 4, but in JUnit 5 (Jupiter), the mechanism has changed.
From the guide:
@Test
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
void failsIfExecutionTimeExceeds500Milliseconds() {
// fails if execution time exceeds 500 milliseconds
}
(The unit
defaults to seconds, so you could just use @Timeout(1)
.)
It's easy to apply once across a class:
To apply the same timeout to all test methods within a test class and all of its @Nested classes, you can declare the @Timeout annotation at the class level.
For your purposes, you might even experiment with top-level configuration on your build jobs:
"junit.jupiter.execution.timeout.test.method.default"
Default timeout for @Test methods