The Wayback Machine - https://web.archive.org/web/20240610203149/https://www.geeksforgeeks.org/junit-5-test-suites-with-example/
Open In App

JUnit 5 – Test Suites with Example

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JUnit 5 encourages a modular approach to test creation with its test suites. These suites function as containers, bundling multiple test classes for easier management and collective execution within a single run. In this article, we will learn about JUnit 5 – Test Suites.

Test Suite

In JUnit 5, a test suite is a way to combine multiple test classes and run them together as a single unit. The @Suite annotation on the test class is used to define a test suite. You can also include specific test classes in the suite using annotations like @SelectClasses, @SelectPackages, or @SelectClasspathRoots, that help to filter out the test classes you want to run together. Now we go through an example, to execute a test suite.

Example of JUnit 5 – Test Suites

Here we take a Java program that has methods add() and subtract() to perform addition and subtraction of two numbers. We have written test classes for both methods separately as AdditionTest and SubtractionTest. Now we can run both of them collectively as a single unit, under a test suite CalculatorTestSuite which is annotated by @Suite, and then @SelectClasses which includes both our test classes.

Calculator.java File:

Java




// Calculator.java
public class Calculator {
  
    public static int add(int a, int b) {
        return a + b;
    }
  
    public static int subtract(int a, int b) {
        return a - b;
    }
}


AdditionTest.java File:

Java




// AdditionTest.java
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
  
class AdditionTest {
  
     @Test
     void testAddition() {
            Calculator calculator = new Calculator();
            assertEquals(5, calculator.add(2, 3));
        }
  
}


SubtractionTest.java File:

Java




// SubtractionTest.java
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
  
class SubtractionTest {
  
    @Test
      void testSubtraction()
    {
        Calculator calculator = new Calculator();
        assertEquals(2, calculator.subtract(5, 3));
    }
}


CalculatorTestSuite.java File:

Java




// CalculatorTestSuite.java
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
  
@Suite
@SelectClasses({AdditionTest.class, SubtractionTest.class})
public class CalculatorTestSuite {
    
}


Output:

Output in Java

Output



Similar Reads

JUnit 5 vs JUnit 4
JUnit 4 and JUnit 5 both are Java-based testing frameworks, The JUnit 5 is the advanced testing framework when compared with JUnit 4. The JUnit provides a lot of features like Annotation-based coding, parallel test execution, and other features. Difference between JUnit 5 and JUnit 4Topic JUnit 5 JUnit 4 Architecture JUnit 5 supports designs with m
4 min read
Best Office Suites for Kali Linux in 2023
Office suites are software packages that are used for business or office environments for productivity. This suit consists of various tools like spreadsheets, presentations, word pressing, etc.Office Suites is undoubtedly one of the most important open-source software for a lot of people, and when it comes to Office Suites on Kali Linux, we have a
6 min read
JUnit - assertAll, assumingThat and assumeFalse with Example
The quality of the software is very very important and it can be enhanced by JUNIT test cases. There are a lot of useful methods available for validating user input. Any login web application has two fields namely EmailId and password. In this article, let us see how to check the validity of email and password as well as test the same using JUNIT.
4 min read
JUnit 5 – @AfterAll Annotation with Example
JUnit 5 is a widely used testing framework in the Java ecosystem. It is the successor of JUnit 4 and is designed to address its limitations. JUnit framework allows the developers to write and run the tests for their Java code. These tests help ensure that the code functions correctly and continues to work as expected as changes are made. JUnit 5 pr
4 min read
JUnit 5 - XML Report Example
In Spring Boot, JUnit can be used for unit testing frameworks in Java, and Junit5 is the latest version. Junit provides a set of tools and APIs for developers to use to write and execute automated tests, and it is primarily used for unit testing Java applications. It is an open-source testing framework. Key Terminologies: Test case: Test cases can
6 min read
WireMock - Stub Priority with JUnit Test
In WireMock, Stub priority allows us to assign priorities for the request mappings. That means it’s a simple way to resolve which stub mapping should be given more precedence if a request matches 2 or more stub mappings. It is sometimes the case that you’ll want to declare two or more stub mappings that “overlap”, in that a given request would be a
4 min read
WireMock - Request Matching with JUnit Test
WireMock is a tool for mocking HTTP-based APIs that runs in the unit tests, on the desktop, or in the test environment. Read more about WireMock in this article: Introduction to WireMock. One of the most important features of WireMock is Request Matching. WireMock supports matching of requests to stubs and verification queries using the following a
5 min read
How to Rerun the Failures in a BDD Junit Test Automation Framework?
We come across failures many times after we execute our test scenarios through automation. These failures can be because of majorly two reasons: Functional Issue (actual failure).Intermittent Issues (Application Slowness, Page not loaded properly, Technical Issue, etc.). Having a mechanism to automatically rerun the failures can be very helpful for
7 min read
Generate Junit Test Cases Using Randoop API in Java
Here we will be discussing how to generate Junit test cases using Randoop along with sample illustration and snapshots of the current instances. So basically in Development If we talk about test cases, then every developer has to write test cases manually. Which is counted in the development effort and also increases the time of the project and cos
4 min read
How to Write Test Cases in Java Application using Mockito and Junit?
Mockito is an open-source testing framework used for unit testing of Java applications. It plays a vital role in developing testable applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in Unit Testing. Unit Testing is a type of software testing in which individual componen
4 min read