Create Test Suite in JUnit
Create Test Suite in JUnit
In this tutorial, let’s learn how to create test suite in JUnit framework. JUnit is a popular Java testing framework used for writing and running unit tests. It provides annotations, assertions, and test runners to automate testing and ensure code correctness. JUnit supports test suites, parameterized tests, and integrations with build tools like Maven and Gradle.
Test Suite in JUnit?
A Test Suite in JUnit is a way to group multiple test classes and run them together as a single unit. This is useful for organizing and executing multiple test cases at once.
We will show you how to create and run a Suite. We will create two test classes TestCaseOne and TestCaseTwo.
TestCaseOne
Create a class as “TestCaseOne”:
/**
*
*/
package com.testingdocs.tutorials;
import org.junit.Test;
/**
*
*/
public class TestCaseOne {
@Test
public void testcaseOne(){
System.out.println("Test Case One");
}
}
TestCaseTwo
Now Create another class “TestCaseTwo” :
/**
*
*/
package com.testingdocs.tutorials;
import org.junit.Test;
/**
*
*/
public class TestCaseTwo {
@Test
public void testcasetwo(){
System.out.println("Test Case Two");
}
}
TestSuite
Create a Java Class called “TestSuite”. Next add @RunWith(Suite.class)
And also Add the reference to Junit test classes using @Suite.SuiteClasses Annotation.
package com.testingdocs.tutorials;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestCaseOne.class,TestCaseTwo.class})
public class TestSuite{
}
@RunWith is a JUnit annotation used to specify a custom runner to execute test cases. It is primarily used in JUnit 4 to customize the way tests are run.
Example:
Uses @RunWith(Suite.class) to execute multiple test classes together.
@RunWith(MockitoJUnitRunner.class) to run Mockito with JUnit.
Output
Now run the Test Suite class as “Run As >> Test Suite(JUnit)”
That’s it.