1. Introduction

This tutorial is going to cover about Timeout test in JUnit 5.  If a test takes too long, can be failed automatically. JUnit 5 provides us some timeout assertions which we can specify the timeout to cause a test method to fail if it takes longer than that timeout.

1. Prerequisites

We will need the following items to be ready on your environment.

  • Java 8
  • JUnit 5
  • Any IDE: Eclipse, IntelliJ IDE

Guides to setup those can be found at: JUnit 5 Basic Introduction – Getting Started With The Next Generation of JUnit

2. Timeout Test in JUnit 5

JUnit 5 comes with 2 main assertions for timeout test:

  • assertTimeout
  • assertTimeoutPreemptively

Let’s see the detailed signature of each method:

2.1. assertTimeout

This method asserts that execution of the supplied executable completes before the given timeout is exceeded.

2.2. assertTimeoutPreemptively

This method asserts that execution of the supplied executable completes before the given timeout is exceeded. Note that the executable will be executed in a different thread than that of the calling code. Furthermore, execution of the executable will be preemptively aborted if the timeout is exceeded.

Besides, there are several overloaded methods of the above methods to supply additional messages in case the tests failed by timeouts.

3. JUnit 5 Timeout Test Examples

In this section, we’re going to get through some examples of timeout test in JUnit 5. The sample source code can be found in Github or download via the link: java-source-example.zip

Let’s say we have a class with 2 methods need to be tested as below:

They have no business logic insight but just contain source code to simulate a duration that each method is going to take.  We may have several test cases as below:

3.1. Using the assertTimeout method

Another example that uses an overload of assertTimeout method:
We have just supplied a message for the assertTimeout method and the message will be printed out if the test fails.

3.2. Using the assertTimeoutPreemptively method

We have used the assertTimeoutPreemptively method and supplied it a message in case the test fails. Note that when we use this assertion method, the printShippingLabel method will be aborted if its execution time is exceeded 15 seconds.

3.3. The test result in IntelliJ

Timeout Test in JUnit 5 - Test results in Intellij

Test results in IntelliJ

4. Conclusion

The tutorial has shown us about timeout test in JUnit 5 with 2 main supported assertions. Besides, there are other overloaded methods which support additional messages in case the test fails.

Below are other articles related to JUnit 5 for your references:

JUnit 5 Tutorial

JUnit 5 Annotations Example

JUnit 5 Basic Introduction – Getting Started With The Next Generation of JUnit

JUnit 5 Maven Example

JUnit 5 vs JUnit 4

JUnit 5 Assertions Example

JUnit 5 Disable or Ignore A Test

JUnit 5 Exception Testing

JUnit 5 Dynamic Tests – Generate Tests at Run-time

JUnit 5 Nested Tests Examples

JUnit 5 Test Suite – Aggregating Tests In Suites

JUnit 5 Assumptions With Assume

JUnit 5 Parameter Resolution Example

0 0 vote
Article Rating