0
0
JUnittesting~15 mins

@Execution annotation in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify that tests run concurrently using @Execution(CONCURRENT)
Preconditions (2)
Step 1: Annotate the test class with @Execution(ExecutionMode.CONCURRENT)
Step 2: Create at least two test methods that each sleep for 2 seconds and then assert true
Step 3: Run the test class
Step 4: Observe the total execution time
✅ Expected Result: The total execution time should be close to 2 seconds, indicating tests ran concurrently
Automation Requirements - JUnit 5
Assertions Needed:
Assert that each test method completes successfully
Verify that total execution time is less than sum of individual test durations (indicating concurrency)
Best Practices:
Use @Execution annotation at class level
Use Assertions.assertTrue for simple assertions
Avoid Thread.sleep in production tests, but acceptable here for demonstration
Use System.nanoTime() or System.currentTimeMillis() to measure execution time
Automated Solution
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Execution(ExecutionMode.CONCURRENT)
public class ConcurrentExecutionTest {

    @Test
    void testOne() throws InterruptedException {
        Thread.sleep(2000);
        assertTrue(true);
    }

    @Test
    void testTwo() throws InterruptedException {
        Thread.sleep(2000);
        assertTrue(true);
    }

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        org.junit.platform.launcher.LauncherDiscoveryRequest request =
            org.junit.platform.engine.discovery.DiscoverySelectors.selectClass(ConcurrentExecutionTest.class);

        org.junit.platform.launcher.Launcher launcher = org.junit.platform.launcher.LauncherFactory.create();
        launcher.execute(
            org.junit.platform.launcher.LauncherDiscoveryRequestBuilder.request()
                .selectors(org.junit.platform.engine.discovery.DiscoverySelectors.selectClass(ConcurrentExecutionTest.class))
                .build()
        );
        long end = System.currentTimeMillis();
        long duration = end - start;
        System.out.println("Total execution time (ms): " + duration);
        assertTrue(duration < 3500, "Tests did not run concurrently as expected");
    }
}

The test class ConcurrentExecutionTest is annotated with @Execution(ExecutionMode.CONCURRENT) to tell JUnit to run tests in parallel.

Two test methods each sleep for 2 seconds and then assert true to simulate work.

The main method runs the tests programmatically and measures total execution time.

If tests run sequentially, total time would be about 4000 ms (2 tests * 2 seconds each).

If tests run concurrently, total time should be close to 2000 ms.

The assertion checks that total time is less than 3500 ms to confirm concurrency.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not annotating the test class or methods with @Execution annotation', 'why_bad': "JUnit will run tests sequentially by default, so concurrency won't happen", 'correct_approach': 'Add @Execution(ExecutionMode.CONCURRENT) at class or method level to enable parallel execution'}
{'mistake': 'Using Thread.sleep without handling InterruptedException', 'why_bad': 'Code will not compile or tests will fail unexpectedly', 'correct_approach': "Declare test methods with 'throws InterruptedException' or handle exception properly"}
Measuring execution time incorrectly or outside test run
Bonus Challenge

Now add three test methods with different sleep durations and verify that total execution time is close to the longest sleep time

Show Hint