{"id":5436,"date":"2025-02-15T16:10:33","date_gmt":"2025-02-15T16:10:33","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5436"},"modified":"2025-02-15T16:28:06","modified_gmt":"2025-02-15T16:28:06","slug":"parallel-test-in-junit","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/parallel-test-in-junit\/","title":{"rendered":"Parallel Test in Junit"},"content":{"rendered":"<p><strong>BaseUnitTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.junit.jupiter.api.AfterEach;\r\nimport org.junit.jupiter.api.BeforeEach;\r\n\r\nimport java.util.concurrent.TimeUnit;\r\n\r\npublic class BaseUnitTest {\r\n    long startTime;\r\n    long endTime;\r\n\r\n    @BeforeEach\r\n    public void recordStartTime() {\r\n        startTime = System.currentTimeMillis();\r\n    }\r\n\r\n    @AfterEach\r\n    public void recordEndAndExecutionTime() {\r\n        endTime = System.currentTimeMillis();\r\n        System.out.println(&quot;Last testcase exection time in millisecond : &quot; + \r\n                              TimeUnit.NANOSECONDS.toMicros((endTime - startTime)) + &quot; Seconds&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>FirstTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.junit.jupiter.api.Test;\r\n\r\nclass FirstTest extends BaseUnitTest{\r\n    @Test\r\n    void oneSecondTest() throws InterruptedException {\r\n        System.out.println(&quot;oneSecondTest() name  =&gt;  &quot; + Thread.currentThread().getName());\r\n        Thread.sleep(1000);\r\n    }\r\n\r\n    @Test\r\n    void twoSecondTest() throws InterruptedException {\r\n        System.out.println(&quot;twoSecondTest() name =&gt; &quot; + Thread.currentThread().getName());\r\n        Thread.sleep(2000);\r\n    }\r\n\r\n    @Test\r\n    void threeSecondTest() throws InterruptedException {\r\n        System.out.println(&quot;threeSecondTest() name =&gt; &quot; + Thread.currentThread().getName());\r\n        Thread.sleep(3000);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SecondTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.junit.jupiter.api.Test;\r\n\r\npublic class SecondTest extends BaseUnitTest{\r\n    @Test\r\n    void oneSecondTest() throws InterruptedException {\r\n        System.out.println(&quot;oneSecondTest() name =&gt; &quot; + Thread.currentThread().getName());\r\n        Thread.sleep(1000);\r\n    }\r\n\r\n    @Test\r\n    void twoSecondTest() throws InterruptedException {\r\n        System.out.println(&quot;twoSecondTest() name =&gt; &quot; + Thread.currentThread().getName());\r\n        Thread.sleep(2000);\r\n    }\r\n\r\n    @Test\r\n    void threeSecondTest() throws InterruptedException {\r\n        System.out.println(&quot;threeSecondTest() name =&gt; &quot; + Thread.currentThread().getName());\r\n        Thread.sleep(3000);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>ThirdTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.junit.jupiter.api.Test;\r\n\r\npublic class ThirdTest extends BaseUnitTest{\r\n    @Test\r\n    void oneSecondTest() throws InterruptedException {\r\n        System.out.println(&quot;oneSecondTest() name =&gt; &quot; + Thread.currentThread().getName());\r\n        Thread.sleep(1000);\r\n    }\r\n\r\n    @Test\r\n    void twoSecondTest() throws InterruptedException {\r\n        System.out.println(&quot;twoSecondTest() name =&gt; &quot; + Thread.currentThread().getName());\r\n        Thread.sleep(2000);\r\n    }\r\n\r\n    @Test\r\n    void threeSecondTest() throws InterruptedException {\r\n        System.out.println(&quot;threeSecondTest() name =&gt; &quot; + Thread.currentThread().getName());\r\n        Thread.sleep(3000);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nLast testcase exection time in millisecond : 0 Seconds\r\nLast testcase exection time in millisecond : 2 Seconds\r\nLast testcase exection time in millisecond : 2 Seconds\r\nLast testcase exection time in millisecond : 2 Seconds\r\nLast testcase exection time in millisecond : 3 Seconds\r\nLast testcase exection time in millisecond : 3 Seconds\r\nLast testcase exection time in millisecond : 3 Seconds\r\nthreeSecondTest() name => ForkJoinPool-1-worker-10\r\ntwoSecondTest() name => ForkJoinPool-1-worker-7\r\noneSecondTest() name  =>  ForkJoinPool-1-worker-1\r\ntwoSecondTest() name => ForkJoinPool-1-worker-6\r\noneSecondTest() name => ForkJoinPool-1-worker-3\r\nthreeSecondTest() name => ForkJoinPool-1-worker-5\r\noneSecondTest() name => ForkJoinPool-1-worker-4\r\ntwoSecondTest() name => ForkJoinPool-1-worker-8\r\nthreeSecondTest() name => ForkJoinPool-1-worker-9\r\n<\/pre>\n<p><strong>junit-platform.properties<\/strong><\/p>\n<pre>\r\n# Enable parallelism\r\njunit.jupiter.execution.parallel.enabled = true\r\n\r\n# Enable parallelism for both test methods and classes\r\njunit.jupiter.execution.parallel.mode.default =  concurrent\r\n<\/pre>\n<p>In Maven Command<\/p>\n<pre>\r\n>>mvn test -Djunit.jupiter.execution.parallel.enabled=true  -Djunit.jupiter.execution.parallel.mode.default=concurrent\r\n<\/pre>\n<p>Four possibilities in <strong>junit-platform.properties<\/strong> config<\/p>\n<pre>\r\n\/\/Only One class and One method in that class would be executed - Sequential Execution\r\njunit.jupiter.execution.parallel.mode.default =  same_thread\r\njunit.jupiter.execution.parallel.mode.classes.default = same_thread\r\n\r\n\/\/More than one class would run in parallel but only one method in each class would be executed \r\n\/\/Test method within one class run sequentially but more than one class run in parallel\r\njunit.jupiter.execution.parallel.mode.default =  same_thread\r\njunit.jupiter.execution.parallel.mode.classes.default = concurrent\r\n\r\n\/\/Only One class and Multiple method in that class would be executed\r\njunit.jupiter.execution.parallel.mode.default =  concurrent\r\njunit.jupiter.execution.parallel.mode.classes.default = same_thread\r\n\r\n\/\/Multiple classes and Multiple method in  those classes would be executed\r\njunit.jupiter.execution.parallel.mode.default =  concurrent\r\njunit.jupiter.execution.parallel.mode.classes.default = concurrent\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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(&quot;Last testcase exection time in millisecond : &quot; + TimeUnit.NANOSECONDS.toMicros((endTime &#8211; startTime)) + &quot; Seconds&quot;); } } FirstTest.java import org.junit.jupiter.api.Test; class FirstTest extends&hellip; <a href=\"https:\/\/codethataint.com\/blog\/parallel-test-in-junit\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[331],"tags":[],"class_list":["post-5436","post","type-post","status-publish","format-standard","hentry","category-junit5"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5436"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5436\/revisions"}],"predecessor-version":[{"id":5439,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5436\/revisions\/5439"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}