Introduction
In this chapter, we will explore JUnit test suites. A test suite is a collection of test classes and test methods that can be run together. Test suites help organize and manage related tests, making it easier to run multiple tests and get an overview of the test results.
In JUnit 5, test suites are created using the @Suite
annotation from the junit-platform-suite
library.
What is a Test Suite?
A test suite is a way to group multiple test classes so that they can be executed together. This is particularly useful for large projects with many test classes, allowing you to run all related tests in a single operation.
Creating a Test Suite in JUnit 5
JUnit 5 provides several annotations to create test suites, such as @SelectClasses
, @SelectPackages
, and @IncludeTags
.
Steps to Create a Test Suite
To create a test suite in JUnit 5, you will:
- Add the required JUnit 5 dependencies
- Create Classes Under Test: These are the classes containing the actual business logic that you want to test.
- Create Test Classes: These classes contain the test methods for the classes under test. Each test class usually corresponds to one class under test.
- Create the Test Suite Class: This class will use JUnit 5 annotations to group multiple test classes into a single suite.
Step 1: Add the Maven Dependencies
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.3</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.10.3</version>
<scope>test</scope>
</dependency>
</dependencies>
- JUnit Jupiter API (
junit-jupiter-api
): This dependency provides the JUnit 5 annotations and assertions. It includes the core API for writing tests. - JUnit Jupiter Engine (
junit-jupiter-engine
): This dependency is the runtime engine that executes JUnit 5 tests. It integrates with your build tool or IDE to run the tests. - JUnit Platform Suite (
junit-platform-suite
): This dependency allows you to define and run test suites using the@Suite
annotation. It provides the necessary classes and annotations to create and manage test suites.
Step 2: Create Classes Under Test
Calculator Class
The Calculator
class will have basic arithmetic operations.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
}
StringUtils Class
The StringUtils
class will have methods for reversing a string and checking if a string is a palindrome.
public class StringUtils {
public String reverse(String input) {
return new StringBuilder(input).reverse().toString();
}
public boolean isPalindrome(String input) {
String reversed = reverse(input);
return input.equals(reversed);
}
}
Step 3: Create Test Classes
CalculatorTest
The CalculatorTest
class will contain tests for the Calculator
class.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CalculatorTest {
private Calculator calculator = new Calculator();
@Test
public void testAdd() {
int result = calculator.add(2, 3);
assertEquals(5, result, "2 + 3 should equal 5");
}
@Test
public void testSubtract() {
int result = calculator.subtract(5, 3);
assertEquals(2, result, "5 - 3 should equal 2");
}
}
StringUtilsTest
The StringUtilsTest
class will contain tests for the StringUtils
class.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class StringUtilsTest {
private StringUtils stringUtils = new StringUtils();
@Test
public void testReverse() {
String result = stringUtils.reverse("hello");
assertEquals("olleh", result, "The reverse of 'hello' should be 'olleh'");
}
@Test
public void testIsPalindrome() {
boolean result = stringUtils.isPalindrome("racecar");
assertTrue(result, "'racecar' should be a palindrome");
}
@Test
public void testIsNotPalindrome() {
boolean result = stringUtils.isPalindrome("hello");
assertFalse(result, "'hello' should not be a palindrome");
}
}
Step 4: Create the Test Suite Class
Create a new class named AllTests
and use the @Suite
annotation to include the test classes.
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
@Suite
@SelectClasses({ CalculatorTest.class, StringUtilsTest.class })
public class AllTests {
// This class remains empty, it is used only as a holder for the above annotations
}
Explanation:
@Suite
: This annotation marks the class as a test suite.@SelectClasses({ CalculatorTest.class, StringUtilsTest.class })
: This annotation includes theCalculatorTest
andStringUtilsTest
classes in the test suite.
Running the Test Suite
To run the test suite, simply run the AllTests
class as a JUnit test. This will execute all the test methods in the included test classes.
Using IntelliJ IDEA
- Run Test Suite: Click the green run icon next to the
AllTests
class and selectRun
. - View Results: The results will be displayed in the Run window. A green check mark indicates that all tests passed, while a red cross indicates that some tests failed.
Conclusion
JUnit test suites are a powerful way to organize and manage your tests. By grouping related test classes, you can run all relevant tests together and get a comprehensive view of your test results. JUnit 5 provides flexible and powerful annotations to create and configure test suites, making it easier to manage large test sets in your Java projects.