Test Overview
This test checks if a method completes its execution within a specified time limit using assertTimeout in JUnit. It verifies the performance by ensuring the method does not exceed the allowed duration.
This test checks if a method completes its execution within a specified time limit using assertTimeout in JUnit. It verifies the performance by ensuring the method does not exceed the allowed duration.
import static org.junit.jupiter.api.Assertions.assertTimeout; import java.time.Duration; import org.junit.jupiter.api.Test; public class PerformanceTest { public void performTask() { // Simulate a task that takes some time try { Thread.sleep(500); // 500 milliseconds } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } @Test public void testPerformTaskCompletesWithinOneSecond() { assertTimeout(Duration.ofSeconds(1), () -> { performTask(); }); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized | - | PASS |
| 2 | JUnit invokes testPerformTaskCompletesWithinOneSecond() | Test method ready to execute | - | PASS |
| 3 | assertTimeout starts timer with 1 second limit | Timer running to measure execution duration | - | PASS |
| 4 | performTask() method executes and sleeps for 500 milliseconds | Simulated task running | - | PASS |
| 5 | performTask() completes within 500 milliseconds | Task finished before timeout | Execution time <= 1 second | PASS |
| 6 | assertTimeout verifies execution time is within limit | Test method execution complete | assertTimeout passes | PASS |
| 7 | Test finishes successfully | JUnit reports test passed | - | PASS |