Introduction
Welcome readers! In this blog, we will learn about the Time test in JUnit. We will learn about Timeout for tests. We need Timeout for tests since we not only want tests to pass, but we also want them to pass within a particular time. We will see some example test codes of timeout. Finally, we will learn about global timeout and see an example of global timeout.
Let's get started, and I hope you learn a lot from this tutorial.
Timeout for Tests
A timeout is a convenient option that JUnit provides. If a test takes more time than the time specified, JUnit will mark this test as failed, and the failure is triggered by an Exception being thrown. We provide this specific value of timeout in milliseconds in @Test annotation.
We use the @Test timeout attribute for testable methods as well as all the testable methods within one class and its nested class.
Timeout uses threading for inner implementation. so if a test takes longer than the time specified in the timeout value, the test will fail, and JUnit will interrupt the thread running that test.
Need of Timeouts
Timeouts are essential when we want to check the efficiency of a method under test, so to check if the code is running under given time constraints, check if code is not stuck in an infinite loop anywhere we use Timeouts.
We also need Timeouts to test for the fail-safe behavior of the application and how the application behaves when the external systems do not respond in the given time.
Therefore, when mocking any external dependencies, it is advisable to use timeout-related tests. This is also considered JUnit best practice to be followed.
Some Default Timeouts
Timeout will not be used if @Test annotation is not present, to use a timeout in such a case we have to provide timeout via one of the following parameters.
-
Junit.jupiter.execution.timeout.default
Default timeout for all testable and lifecycle methods -
Junit.jupiter.execution.timeout.testable.method.default
Default timeout for all testable methods -
Junit.jupiter.execution.timeout.test.method.default
Default timeout for @Test methods -
Junit.jupiter.execution.timeout.testtemplate.method.default
Default timeout for @TestTemplate methods -
Junit.jupiter.execution.timeout.testfactory.method.default
Default timeout for @TestFactory methods -
Junit.jupiter.execution.timeout.lifecycle.method.default
Default timeout for all lifecycle methods -
Junit.jupiter.execution.timeout.beforeall.method.default
Default timeout for @BeforeAll methods -
Junit.jupiter.execution.timeout.beforeeach.method.default
Default timeout for @BeforeEach methods -
Junit.jupiter.execution.timeout.aftereach.method.default
Default timeout for @AfterEach methods -
Junit.jupiter.execution.timeout.afterall.method.default
Default timeout for @AfterAll methods
Example
Now let’s see examples to better understand the concepts of Timeout.
Program
Let’s create an example test class. In the @test annotation, we have specified the timeout value to be 500 milliseconds. While the testTimeout() function will take more than 1 second to execute since it is sleeping for 1 second, which is why the test will fail.
package com.aditya04848.junit.helper;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
public class TimeoutTest {
@Test(timeout = 500)
public void testTimeout() throws InterruptedException
{
TimeUnit.SECONDS.sleep(1);
}
}Output
Program
Here in this test, the timeout value is 1500 milliseconds, hence the test will pass.
package com.aditya04848.junit.helper;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
public class TimeoutTest {
@Test(timeout = 1500)
public void testTimeout() throws InterruptedException
{
TimeUnit.SECONDS.sleep(1);
}
}






