Test Overview
This test measures how long a simple method takes to execute and verifies that it completes within a set time limit.
This test measures how long a simple method takes to execute and verifies that it completes within a set time limit.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTimeout; import java.time.Duration; public class ExecutionTimeTest { @Test public void testMethodExecutionTime() throws InterruptedException { assertTimeout(Duration.ofMillis(500), () -> { // Simulate some work with sleep Thread.sleep(300); }); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized | — | PASS |
| 2 | JUnit invokes testMethodExecutionTime() | Inside test method | — | PASS |
| 3 | assertTimeout starts timer and runs lambda | Timer running, lambda executing | — | PASS |
| 4 | Lambda executes Thread.sleep(300) to simulate work | Thread sleeping for 300 milliseconds | — | PASS |
| 5 | Lambda completes, assertTimeout checks elapsed time | Elapsed time measured | Elapsed time <= 500 milliseconds | PASS |
| 6 | Test finishes successfully within time limit | Test passed | Test execution time is acceptable | PASS |
long start = System.nanoTime(); Thread.sleep(100); long end = System.nanoTime(); System.out.println((end - start) / 1_000_000 + " ms");
@Test
void testMethod() {
long start = System.nanoTime();
// test code
long end = System.nanoTime();
System.out.println(end - start);
}Pass the test and this pattern moves unknown