Execution of JUnit Test Suite

In this blogpost, we will learn how to create a Test Suite using JUnit, and execute multiple test cases using JUnit annotations, classes and methods.

In general terms, Test Suite is a collection of multiple test cases. JUnit allows us to create a Test Suite, and execute multiple test cases together.

JUnit provides several annotations, classes and methods to create and run a Test Suite.

1. @RunWith(Suite.class):

This is a class-level annotation provided in JUnit API. When the class is annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.

2. @Suite.SuiteClasses({TestOne.class, TestTwo.class,…TestN.class}):

This is again a class-level annotation added in JUnit API. It allows us to specify multiple test classes to be included in a Test Suite.

3. JUnitCore:

JUnitCore is a class, a facade to run the JUnit tests.JUnitCore provides runClasses method to run the Test Suite.Result result = JUnitCore.runClasses(TestSuite.class): It is a method implemented in JUnitCore class, that runs the Test Suite specified as a parameter. The method stores the result of execution in Result class instance.

4. Result:

It is a class, that collects and summarizes information from running a test suite/ multiple test cases. Result class provides various methods to analyze the execution status of the last run.

Go through the below steps for understanding the implementation of Test Suite.

  1. Creating multiple test case classes
  2. Creating a Test suite class
  3. Creating a Test Runner class to execute the Test suite

The first step is to create two test case classes.

TestCaseOne.java: The test case file contains only one test method.

import org.junit.BeforeClass;
import org.junit.Test;

public class TestCaseOne {

@BeforeClass
public static void beforeTestCaseOneClass(){
System.out.println(“————————————————“);
System.out.println();
System.out.println(“Executing Test methods of Test Case One class…”);
System.out.println(“————————————————“);
System.out.println();
}

@Test
public void testOne(){
System.out.println(“Inside Test One method of Test case one class”);
}

@Test
public void testTwo(){
System.out.println(“Inside Test Two method of Test case one class”);
}

}

TestCaseTwo.java: The test case file contains only one test method.

import org.junit.BeforeClass;
import org.junit.Test;

public class TestCaseTwo {

@BeforeClass
public static void beforeTestCaseTwoClass() {
System.out.println(“————————————————“);
System.out.println();
System.out.println(“Executing Test methods of Test Case Two class…”);
System.out.println(“————————————————“);
System.out.println();

}

@Test
public void sampleOne() {
System.out.println(“Inside Sample One method of Test case two class”);
}

@Test
public void sampleTwo() {
System.out.println(“Inside Sample Two method of Test case two class”);
}
}

Now, the next step is to create a Test Suite class, in order to run the above two test case classes together.

TestSuite.java: JUnit provides two annotations to create a Test Suite.

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ TestCaseOne.class, TestCaseTwo.class })
public class TestSuite {

}

Now, we need to create one more Test Runner class to execute the above TestSuite class.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunnerClass {

public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestSuite.class);

for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}

System.out.println();
System.out.println(“Test Suite execution was successful: “+ result.wasSuccessful());
}
}

That is it!

Right click on the TestSuite class, and Run As -> JUnit Test.
execute-test-suite-using-junit

The another way of executing the test suite is by running the TestRunner class.

Right click on TestRunner class, and Run As -> Java ApplicationExecute-Test-suite-using-junit-test-runner.PNG

Executing the Test Suite with the use of Test Runner class allows us to utilize the methods provided in Test Runner class, that allows us to analyze the execution status of the last run.

Hope it helps you!!

Cheers 🙂

Advertisements

#runwith, #suite-suiteclasses, #execute-multiple-test-cases-using-junit, #junit-test-suite-execution, #junitcore, #junitcore-runclasses, #result, #result-getfailures, #result-wassuccessful, #test-runner