We have a scenario where we have 3 java files having Test scripts. Instead of executing the Java files individually. We can execute the 3 Java files at one go,meaning with single file we can execute all our 3 Java files.To perform this task, we go for Batch Run concept in Junit.
Below are the 3 Java files
Test1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.junit.Test; public class Test1 { @Test public void testMethod1() { System.out.println("testmethod1 executed"); } @Test public void testMethod2() { System.out.println("testmethod2 executed"); } } |
Test2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.junit.Test; public class Test2 { @Test public void testMethod3() { System.out.println("testmethod3 executed"); } @Test public void testMethod4() { System.out.println("testmethod4 executed"); } } |
Test3.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.junit.Test; public class Test3 { @Test public void testMethod5() { System.out.println("testmethod5 executed"); } @Test public void testMethod6() { System.out.println("testmethod6 executed"); } } |
Instead of executing Test1,Test2,Test3 Java classes individually. Junit has provided an option to execute all the Test1.java,Test2.java,Test3.java at one stretch.
Example on Batch Run Execution using Junit :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ Test1.class, //test case 1 Test2.class, //test case 2 Test3.class //test case 3 }) public class batchRun_Junit { //We will have this as an empty class } |
OUTPUT :
We have a scenario where we have 3 java files having Test scripts. Instead of executing the Java files individually. We can execute the 3 Java files at one go,meaning with single file we can execute all our 3 Java files.To perform this task, we go for Batch Run concept in Junit.
Below are the 3 Java files
Test1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.junit.Test; public class Test1 { @Test public void testMethod1() { System.out.println("testmethod1 executed"); } @Test public void testMethod2() { System.out.println("testmethod2 executed"); } } |
Test2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.junit.Test; public class Test2 { @Test public void testMethod3() { System.out.println("testmethod3 executed"); } @Test public void testMethod4() { System.out.println("testmethod4 executed"); } } |
Test3.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.junit.Test; public class Test3 { @Test public void testMethod5() { System.out.println("testmethod5 executed"); } @Test public void testMethod6() { System.out.println("testmethod6 executed"); } } |
Instead of executing Test1,Test2,Test3 Java classes individually. Junit has provided an option to execute all the Test1.java,Test2.java,Test3.java at one stretch.
Example on Batch Run Execution using Junit :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ Test1.class, //test case 1 Test2.class, //test case 2 Test3.class //test case 3 }) public class batchRun_Junit { //We will have this as an empty class } |
OUTPUT :