What if you could speed up your tests with just one simple annotation?
Why @Execution annotation in JUnit? - Purpose & Use Cases
Imagine you have many test cases to run, but you want to control if they run one after another or all at once. Doing this by hand means running tests one by one or opening many windows to run them all, which is confusing and slow.
Manually managing test execution order or parallelism is slow and error-prone. You might forget to run some tests, or tests might interfere with each other if run at the same time. It's hard to keep track and wastes time.
The @Execution annotation lets you easily tell JUnit how to run your tests: either all in order or in parallel. This saves time and avoids mistakes by automating test execution control.
Run tests one by one manually or open multiple consoles to run tests in parallel.
@Execution(ExecutionMode.CONCURRENT)
public class MyTests { ... }You can run tests faster and more reliably by simply adding @Execution to control parallel or sequential execution.
When testing a web app, you want to run many UI tests at the same time to save time, but some tests must run in order to avoid conflicts. @Execution helps you set this easily.
Manual test running is slow and error-prone.
@Execution controls test run order and parallelism.
This makes testing faster and more reliable.
Practice
@Execution annotation in JUnit?Solution
Step 1: Understand the role of @Execution
The@Executionannotation controls how tests are run: either all at once (concurrently) or one after another (sequentially).Step 2: Differentiate from other annotations
It does not set order, disable tests, or set timeouts. Those are handled by other annotations like@Order,@Disabled, or@Timeout.Final Answer:
To control whether tests run concurrently or sequentially -> Option DQuick Check:
@Execution controls test concurrency = A [OK]
- Confusing @Execution with @Order
- Thinking it disables tests
- Assuming it sets test timeouts
@Execution in JUnit?Solution
Step 1: Recall valid ExecutionMode values
JUnit definesExecutionMode.CONCURRENTandExecutionMode.SAME_THREAD. There is noSEQUENTIALorPARALLELmode.Step 2: Identify the mode for concurrent execution
ExecutionMode.CONCURRENTruns tests in parallel threads.Final Answer:
@Execution(ExecutionMode.CONCURRENT) -> Option AQuick Check:
Concurrent mode = CONCURRENT [OK]
- Using non-existent ExecutionMode values
- Confusing SAME_THREAD with concurrent
- Misspelling ExecutionMode constants
@Execution(ExecutionMode.CONCURRENT)
class SampleTest {
@Test void testA() { System.out.println("A"); }
@Test void testB() { System.out.println("B"); }
@Test void testC() { System.out.println("C"); }
}Solution
Step 1: Understand ExecutionMode.CONCURRENT effect
With@Execution(ExecutionMode.CONCURRENT), tests run in parallel threads, so their output order is not guaranteed.Step 2: Analyze output behavior
Since threads run independently, print statements may appear in any order depending on thread scheduling.Final Answer:
Tests run concurrently; order of output is unpredictable -> Option BQuick Check:
Concurrent = unpredictable order [OK]
- Assuming concurrent means ordered
- Thinking output is alphabetical
- Confusing concurrent with sequential execution
@Execution:
@Execution(ExecutionMode.CONCURRENT)
class TestExample {
private int counter = 0;
@Test
void increment() {
counter++;
assertEquals(1, counter);
}
@Test
void decrement() {
counter--;
assertEquals(-1, counter);
}
}Solution
Step 1: Analyze shared state usage
Thecountervariable is shared and modified by both tests without synchronization.Step 2: Understand concurrency impact
Running tests concurrently causes race conditions, leading to unpredictable counter values and test failures.Final Answer:
Shared mutable state causes race conditions in concurrent tests -> Option AQuick Check:
Concurrent tests + shared state = race condition [OK]
- Ignoring thread safety in concurrent tests
- Assuming tests run sequentially by default
- Confusing @Execution with @Order annotation
@Execution to optimize test speed while avoiding conflicts?Solution
Step 1: Identify tests with shared data modification
Tests that modify shared data must run sequentially to avoid conflicts.Step 2: Apply @Execution selectively
Use@Execution(ExecutionMode.SAME_THREAD)on tests with shared data, andCONCURRENTon others to speed up execution.Final Answer:
Annotate tests that modify shared data with @Execution(ExecutionMode.SAME_THREAD) and others with CONCURRENT -> Option CQuick Check:
Mix SAME_THREAD for shared data, CONCURRENT for others [OK]
- Running all tests concurrently ignoring shared data
- Running all tests sequentially losing speed benefits
- Not using @Execution selectively per test
Ready to prove it?
Pass the test and this pattern moves unknown
