BaseUnitTest.java
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import java.util.concurrent.TimeUnit; public class BaseUnitTest { long startTime; long endTime; @BeforeEach public void recordStartTime() { startTime = System.currentTimeMillis(); } @AfterEach public void recordEndAndExecutionTime() { endTime = System.currentTimeMillis(); System.out.println("Last testcase exection time in millisecond : " + TimeUnit.NANOSECONDS.toMicros((endTime - startTime)) + " Seconds"); } }
FirstTest.java
import org.junit.jupiter.api.Test; class FirstTest extends BaseUnitTest{ @Test void oneSecondTest() throws InterruptedException { System.out.println("oneSecondTest() name => " + Thread.currentThread().getName()); Thread.sleep(1000); } @Test void twoSecondTest() throws InterruptedException { System.out.println("twoSecondTest() name => " + Thread.currentThread().getName()); Thread.sleep(2000); } @Test void threeSecondTest() throws InterruptedException { System.out.println("threeSecondTest() name => " + Thread.currentThread().getName()); Thread.sleep(3000); } }
SecondTest.java
import org.junit.jupiter.api.Test; public class SecondTest extends BaseUnitTest{ @Test void oneSecondTest() throws InterruptedException { System.out.println("oneSecondTest() name => " + Thread.currentThread().getName()); Thread.sleep(1000); } @Test void twoSecondTest() throws InterruptedException { System.out.println("twoSecondTest() name => " + Thread.currentThread().getName()); Thread.sleep(2000); } @Test void threeSecondTest() throws InterruptedException { System.out.println("threeSecondTest() name => " + Thread.currentThread().getName()); Thread.sleep(3000); } }
ThirdTest.java
import org.junit.jupiter.api.Test; public class ThirdTest extends BaseUnitTest{ @Test void oneSecondTest() throws InterruptedException { System.out.println("oneSecondTest() name => " + Thread.currentThread().getName()); Thread.sleep(1000); } @Test void twoSecondTest() throws InterruptedException { System.out.println("twoSecondTest() name => " + Thread.currentThread().getName()); Thread.sleep(2000); } @Test void threeSecondTest() throws InterruptedException { System.out.println("threeSecondTest() name => " + Thread.currentThread().getName()); Thread.sleep(3000); } }
Output
Last testcase exection time in millisecond : 0 Seconds Last testcase exection time in millisecond : 2 Seconds Last testcase exection time in millisecond : 2 Seconds Last testcase exection time in millisecond : 2 Seconds Last testcase exection time in millisecond : 3 Seconds Last testcase exection time in millisecond : 3 Seconds Last testcase exection time in millisecond : 3 Seconds threeSecondTest() name => ForkJoinPool-1-worker-10 twoSecondTest() name => ForkJoinPool-1-worker-7 oneSecondTest() name => ForkJoinPool-1-worker-1 twoSecondTest() name => ForkJoinPool-1-worker-6 oneSecondTest() name => ForkJoinPool-1-worker-3 threeSecondTest() name => ForkJoinPool-1-worker-5 oneSecondTest() name => ForkJoinPool-1-worker-4 twoSecondTest() name => ForkJoinPool-1-worker-8 threeSecondTest() name => ForkJoinPool-1-worker-9
junit-platform.properties
# Enable parallelism junit.jupiter.execution.parallel.enabled = true # Enable parallelism for both test methods and classes junit.jupiter.execution.parallel.mode.default = concurrent
In Maven Command
>>mvn test -Djunit.jupiter.execution.parallel.enabled=true -Djunit.jupiter.execution.parallel.mode.default=concurrent
Four possibilities in junit-platform.properties config
//Only One class and One method in that class would be executed - Sequential Execution junit.jupiter.execution.parallel.mode.default = same_thread junit.jupiter.execution.parallel.mode.classes.default = same_thread //More than one class would run in parallel but only one method in each class would be executed //Test method within one class run sequentially but more than one class run in parallel junit.jupiter.execution.parallel.mode.default = same_thread junit.jupiter.execution.parallel.mode.classes.default = concurrent //Only One class and Multiple method in that class would be executed junit.jupiter.execution.parallel.mode.default = concurrent junit.jupiter.execution.parallel.mode.classes.default = same_thread //Multiple classes and Multiple method in those classes would be executed junit.jupiter.execution.parallel.mode.default = concurrent junit.jupiter.execution.parallel.mode.classes.default = concurrent