The @Execution annotation controls whether tests run concurrently or sequentially. It helps manage parallel execution behavior in JUnit 5.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @Execution(ExecutionMode.CONCURRENT) public class SampleTest { @Test void testOne() throws InterruptedException { Thread.sleep(100); System.out.println("Test One"); } @Test void testTwo() throws InterruptedException { Thread.sleep(100); System.out.println("Test Two"); } }
The annotation @Execution(ExecutionMode.CONCURRENT) instructs JUnit to run tests concurrently, so the two tests may overlap in execution time.
@Execution(ExecutionMode.CONCURRENT) actually ran in parallel?To verify parallel execution, you check if the tests overlapped in time. Option C checks if the difference between their end times is less than an expected overlap threshold.
@Execution(ExecutionMode.CONCURRENT) to a test class, but tests still run sequentially. What is the most likely reason?Even if @Execution(ExecutionMode.CONCURRENT) is present, parallel execution must be enabled in the JUnit platform configuration or test runner. Otherwise, tests run sequentially.
@Execution(ExecutionMode.CONCURRENT) and individual test methods are annotated with @Execution(ExecutionMode.SAME_THREAD), what is the effective execution mode for those methods?In JUnit 5, method-level @Execution annotations override class-level ones. So methods with SAME_THREAD run sequentially even if the class is marked CONCURRENT.