Table of contents
1.
Introduction
2.
Timeout for Tests
2.1.
Need of Timeouts
2.2.
Some Default Timeouts
2.3.
Example
2.3.1.
Program
2.3.2.
Output
2.3.3.
Program
2.3.4.
Output
3.
Global Timeout
3.1.
Program
3.2.
Output
3.3.
Program
3.4.
Output
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

JUnit Time Test

Author Aditya Anand
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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);
    }
}
You can also try this code with Online Java Compiler
Run Code

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);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Global Timeout

JUnit Foundation provides a timeout management feature that enables you to specify a configurable global timeout interval. The JUnit framework provides the timeout behavior via the timeout parameter of the @Test annotation. If no timeout parameter has been specified, or if the configured global Timeout specifies a longer interval, JUnit Foundation overrides the @Test annotation with a mutable replacement that specifies the configured timeout parameter.

Rather than specifying the timeout attributes for all the tests separately, we can define JUnit Rule for all tests in a class.

Program

Under the @rule annotation, we are using globalTimeout to specify the Timeout value to be 2 seconds, and our testTimeout() function is sleeping for 3 seconds, hence the test will fail.

package com.aditya04848.junit.helper;
import java.util.concurrent.TimeUnit;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;

public class TimeoutTest {
    @Rule
    public Timeout globalTimeout = Timeout.seconds(2);

    @Test  
    public void testTimeout() throws InterruptedException
    {
        TimeUnit.SECONDS.sleep(3);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Program

Here the testTimeout() function is sleeping for 1 second, hence the test will pass.

package com.aditya04848.junit.helper;
import java.util.concurrent.TimeUnit;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;

public class TimeoutTest {
    @Rule
    public Timeout globalTimeout = Timeout.seconds(2);

    @Test  
    public void testTimeout() throws InterruptedException
    {
        TimeUnit.SECONDS.sleep(1);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

FAQs

  1. What is a JUnit test framework?
    JUnit is a simple, open-source framework that provides features like Assertions for testing expected results, test runners, etc.
     
  2. Should you write automated JUnit tests?
    In the long run, automated tests take less time, also automated tests are more reliable, and hence, with good tests system will be fail-proof.
     
  3. How do @beforemethods work in JUnit?
    The JUnit framework automatically invokes any @Beforemethods before each test is run.
     
  4. How do I run multiple tests in JUnit in order?
    Under @Test annotation, we can combine all the test cases in one place in a specific order. 

Key Takeaways

In conclusion, Time Test is crucial in testing the efficiency of source code. In @test annotation, we can provide the value of the timeout, exceeding which leads to the failure of a test case. We can also set the value of timeout using the global timeout to specify the value of timeout in the global interval. Learn more about JUnit Plug with Apache Ant from this blog, I also encourage you to check out JUnit Packages blog.

Check out JUnit Interview Questions here.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass