π Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
π Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. Overview
Tests that 'runaway' or take too long, can be automatically failed. There are two options for implementing this behavior:
- Timeout parameter on @Test Annotation (applies to test method)
- Global Timeout Management with JUnit Foundation
2. Timeout parameter on @Test Annotation (applies to test method)
Exception
being thrown:@Test(timeout=1000)
public void testWithTimeout() {
...
}
Timeout Rule (applies to all test cases in the test class)
timeout
parameter on an individual Test annotation.:import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
public class HasGlobalTimeout {
public static String log;
private final CountDownLatch latch = new CountDownLatch(1);
@Rule
public Timeout globalTimeout = Timeout.seconds(10); // 10 seconds max per method tested
@Test
public void testSleepForTooLong() throws Exception {
log += "ran1";
TimeUnit.SECONDS.sleep(100); // sleep for 100 seconds
}
@Test
public void testBlockForever() throws Exception {
log += "ran2";
latch.await(); // will block
}
}
Timeout
rule applies to the entire test fixture, including any @Before
or @After
methods. If the test method is in an infinite loop (or is otherwise not responsive to interrupts) then @After
methods will not be called.3. Global Timeout Management with JUnit Foundation
TEST_TIMEOUT
configuration option to the desired default test timeout interval in milliseconds. This timeout specification is applied to every test method that doesn't explicitly specify a longer interval.import org.junit.runner.RunWith;
import com.nordstrom.automation.junit.HookInstallingRunner;
@RunWith(HookInstallingRunner.class)
public class ExampleTest {
...
}
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import java.util.concurrent.TimeUnit;
public class TimeoutRuleTest {
//global timeout rule
@Rule
public Timeout globalTimeout = Timeout.seconds(1);
//This test will be failed, because it will take more than 1 second to finish!
@Test
public void testSlowMethod1() throws InterruptedException {
//...
TimeUnit.SECONDS.sleep(5000);
}
//passed
@Test
public void testSlowMethod2() {
//...
}
}
This page refer to an old release of JUnit Foundation. Since version 4.0.0, this library has been activated via a Java agent instead of relying on a custom runner. The current release (9.1.1) has many refinements, including expanded event model, automatic retry of failed tests, dynamic attachment of classic RunListeners, and parameterized artifact capture.
ReplyDeleteHi thanks for suggestion, i will look into latest release and come up with good articles. Most of the projects (may be projects less than JDK 8) still using JUnit 4 so this tutorial is useful for them.
Delete