EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

JUnit Test Suite

Secondary Sidebar
JUnit Tutorial
  • Junit
    • JUnit 5 Assert
    • JUnit RunWith
    • JUnit AssertThat
    • JUnit Report
    • JUnit BeforeClass
    • JUnit Framework
    • JUnit Data Provider
    • JUnit Runner
    • JUnit 5 Parameterized Tests
    • JUnit BeforeAll
    • JUnit Private Methods
    • JUnit Integration Test
    • JUnit Maven Dependency
    • JUnit Annotations?
    • JUnit Testing
    • JUnit Test Order
    • JUnit 5 RunWith
    • JUnit 4 Maven
    • JUnit Jar
    • JUnit assertEquals
    • JUnit 5 Parameterized Test
    • JUnit Dependency
    • JUnit Fail
    • JUnit Disable Test
    • JUnit Assert
    • JUnit in Maven
    • JUnit 5 Gradle
    • JUnit XML
    • JUnit XML Format
    • JUnit Eclipse
    • JUnit Test Suite
    • JUnit Parameterized Test
    • JUnit assert exception
    • JUnit Code Coverage
    • JUnit Jupiter
    • JUnit Rule
    • JUnit version
    • Junit Interview Questions
Home Software Development Software Development Tutorials JUnit Tutorial JUnit Test Suite

JUnit Test Suite

Introduction to JUnit Test Suite

There could be a condition in the application testing scenario in JUnit that multiple test cases need to be bundled together and executed in one go. Test Suite in JUnit facilitates such bundling in a simple and seamless way.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)

Test cases in a test suite can be homogenous or heterogeneous in nature. As an example, a test suite can either contain only smoke test cases / functional test cases/performance test cases or a mixture of all the test cases in any combination.
Normally the test cases are grouped as per the sequence of the life cycle of an application and in some scenarios, test cases of similar scope of operations are grouped together. One test case may find a place in a multiple test suite. A test plan will have multiple test suites and each suite will have multiple test cases.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How to Create Test Suite?

A pictorial representation of how test suites and test plans are created.

Junit Test case 1

Various test cases are grouped together based on some uniqueness as decided by the developer. It can be pertaining to multiple functionalities of one scenario of an application module or a series of test cases pertaining to one functionality in an application. There could be various test suites in an application and they can become one test plan to be managed by the development/testing team.
A test suite can execute a number of test cases as explained above and any new cases can be added on the fly and it can be executed

TestSuite oursuite = new TestSuite();
oursuite.addtest(new xxxTest(“testMultiply”));
oursuite.addtest(new xxxTest(“testDivide”));

or it can also extract test cases automatically and execute them. The testcase class will have to be passed to the constructor of the TestSuite.

Testsuite oursuite = new Testsuite(xxxTest.class);

The constructor creates our suite with the necessary methods included.

As part of Test Suite, a Java class with @SuiteClasses and @RunWith annotations will have to be created and this will be explained in detail in the below examples.

Steps to Create a TestSuite

The following steps can be followed in creating a Testsuite

First of all, have the class ready that has to be tested by Testuite. The class to be tested may be part of an application or it can be ac critical routine that may be used across many applications.

  • Create as many test case classes as required
  • Test Suite class will have to be created. It is a Java Class with annotation reference @RunWith.
  • Test Classes called in the test suite will have to be referenced using an annotation @Suite.SuiteClasses
  • Create as many test suites as needed using multiple combinations of test cases.
  • To execute test suite, create a test runner class and verify the output.

Methods used and its Details

Method Name Access Specifier, Return Type Details
addTest

(test,test)

Void This method adds a test to a suite
addTestMethod Private void Declares the details
addTestSuite Void It adds the test class to the suite
countTestCases Integer Returns the number of test cases that will get executed
createTest Static test Defines the test case
exceptionToString Private Static Stack trace is converted into string
getName String Name of the suite is returned by this method
GetTestConstructor Static Returns constructor which takes single argument or no argument
Run Void The test suite is run and the results are collected in TestResult
SetName Void The name of the suite is set here
testAt Test Test is returned at the specified index
testCount Integer Number of tests covered in the suite is returned
Tests Tests as enumeration are returned
Warning Private static This method returns the test case which is likely to fail beforehand as a warning

Examples

Creating a Java class that has to be tested. This class accepts a text and prints it as it is and also prints it with a tag.

/*
* This class prints the given message on console.
*/
public class MessPrintx {
private String messagex;
//Constructor
//Test that is passed has to be printed
public MessPrintx(String messagex){
this.messagex = messagex;
}
// prints the message as it is
public String plainMessage(){
System.out.println(messagex);
return messagex;
}
// tag "Hello!" to the message and then prints it
public String tagMessage(){
messagex = "Hello!" + messagex;
System.out.println(messagex);
return messagex;
}
}
Creating first test case -1
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
// test case 1 – TestJunit11
public class TestJunit11 {
String messagex = "RRRRRR";
MessagePrintx messagePrintx = new MessagePrintx(messagex);
@Test // Annotation
public void testPlainMessage() {
System.out.println("Inside testPlainMessage()");
assertEquals(messagex, messagePrintx.plainMessage());
}
}
Creating second test case – 2
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
// Test case -2 TestJunit22
public class TestJunit22 {
String messagex = "RRRRRR";
MessagePrintx messagePrintx = new MessagePrintx(messagex);
@Test // annotation
public void testTagMessage() {
System.out.println("Inside testTagMessage()");
messagex = "Hello!" + "RRRRRR";
assertEquals(messagex,messagePrintx.tagMessage());
}
}
Creating Test Suite
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class) // Annotation is attached
@Suite.SuiteClasses({ // Another annotation is attached
TestJunit11.class,
TestJunit22.class
})
public class JunitTestSuitex {
}
Creating Runner Class to execute the test
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
// Actual execution starts from this line using JunitTestSuitex class defined earlier
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitTestSuitex.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}

Result when executed

2

Conclusion

JUnit Test Suite improves the productivity of the developers and testers by reducing their efforts in testing the various applications scenarios in a single shot as against testing them individually. By this clubbing of test cases, total testing time, as well as time to market, comes down appreciably and the accuracy of the product improves to a greater extent.

Recommended Articles

This is a guide to JUnit Test Suite. Here we discuss the Definition, How to create Test Suite? examples with code implementation. You may also have a look at the following articles to learn more –

  1. JUnit version
  2. JUnit Maven Dependency
  3. JUnit Annotations 
  4. Java JUNIT
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More