To execute a JUnit 5 test suite, you need to use the @Suite and related annotations provided by JUnit Platform. These annotations allow grouping and running multiple test classes together as a single test suite.
Key Annotations Used in JUnit 5 Suite
| Annotation | Description |
|---|---|
| @Suite | Marks the class as a test suite. |
| @SelectClasses | Specifies the test classes to include in the suite. |
| @SelectPackages | (Optional) Runs all tests in the given package. |
| @IncludeTags / @ExcludeTags | (Optional) Filters test methods by tags. |
The test suite must not have any test methods of its own. It just aggregates other test classes.
Maven/Gradle Dependency
JUnit 5 uses the
junit-platform-suite module:<dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite</artifactId> <version>1.10.0</version> <scope>test</scope> </dependency>
Also include JUnit Jupiter:
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.10.0</version> <scope>test</scope> </dependency>
Example: How to Execute JUnit 5 Test Suite
Step 1: Create Sample Test Classes
// File: CalculatorTest.java import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class CalculatorTest { @Test void testAddition() { assertEquals(4, 2 + 2); } @Test void testSubtraction() { assertEquals(0, 2 - 2); } }
// File: StringUtilsTest.java import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class StringUtilsTest { @Test void testToUpperCase() { assertEquals("HELLO", "hello".toUpperCase()); } @Test void testLength() { assertEquals(5, "hello".length()); } }
Step 2: Create Test Suite Class
// File: AllTestsSuite.java import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; @Suite @SelectClasses({ CalculatorTest.class, StringUtilsTest.class }) public class AllTestsSuite { // No code needed here }
How to Run the Suite
Option 1: From IDE (like Eclipse/IntelliJ)
Right-click on
AllTestsSuite.java→ Run As → JUnit Test.
Option 2: Using Maven
Run the tests with:
mvn test
No comments:
Post a Comment