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
1 2 3 |
public static void assertTimeout(Duration timeout, Executable executable) { AssertTimeout.assertTimeout(timeout, executable); } |
2.2. assertTimeoutPreemptively
1 2 3 |
public static void assertTimeoutPreemptively(Duration timeout, Executable executable) { AssertTimeout.assertTimeoutPreemptively(timeout, executable); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class OrderService { public void doPayment() { try { Thread.sleep(10000);// 10 seconds } catch (InterruptedException e) { e.printStackTrace(); } } public void printShippingLabel() { try { Thread.sleep(20000);// 20 seconds } catch (InterruptedException e) { e.printStackTrace(); } } } |
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
1 2 3 4 5 6 7 8 |
@Test public void doPaymentNotExceed15Seconds() { OrderService orderService = new OrderService(); assertTimeout(ofSeconds(15), () -> { // This method runs in 10 seconds orderService.doPayment(); }); } |
1 2 3 4 5 6 7 8 |
@Test public void doPaymentExceed5Seconds() { OrderService orderService = new OrderService(); assertTimeout(ofSeconds(5), () -> { // This method runs in 10 seconds orderService.doPayment(); } , "The doPayment method take more than 5 seconds"); } |
3.2. Using the assertTimeoutPreemptively method
1 2 3 4 5 6 7 8 |
@Test public void printShippingLabelExceeded15SecondsWithPreemptiveTermination() { OrderService orderService = new OrderService(); assertTimeoutPreemptively(ofSeconds(15), () -> { // This method takes 20 seconds to run orderService.printShippingLabel(); } , () -> "The printShippingLabel method took more than 15 seconds and was aborted."); } |
3.3. The test result 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 Basic Introduction – Getting Started With The Next Generation of JUnit
JUnit 5 Disable or Ignore A Test
JUnit 5 Dynamic Tests – Generate Tests at Run-time
JUnit 5 Test Suite – Aggregating Tests In Suites
JUnit 5 Assumptions With Assume
JUnit 5 Parameter Resolution Example