Create and run a JUnit test suite using @Suite
Preconditions (2)
✅ Expected Result: All test methods from the specified test classes run successfully when running the suite class
import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; @Suite @SelectClasses({CalculatorTest.class, StringUtilsTest.class}) public class AllTestsSuite { // This class remains empty, it is used only as a holder for the above annotations } // Example test class 1 import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test void addition() { assertEquals(5, 2 + 3); } } // Example test class 2 import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; public class StringUtilsTest { @Test void isEmptyReturnsTrueForEmptyString() { assertTrue("".isEmpty()); } }
The AllTestsSuite class is annotated with @Suite and @SelectClasses to specify which test classes to run together.
The suite class itself has no methods; it just groups the tests.
Two example test classes, CalculatorTest and StringUtilsTest, each have simple test methods with assertions.
Running AllTestsSuite will run all tests from both classes, verifying the suite setup works.
Add a third test class to the suite and verify all three classes run together
@Suite in JUnit testing?@Suite and @SelectClasses?AllTests?
@Suite
@SelectClasses({TestOne.class, TestTwo.class})
public class AllTests {}@Suite
@SelectClasses({TestA.class, TestB.class})
public void AllTests() {}LoginTests, PaymentTests, and LogoutTests classes. Which of the following is the best way to write this suite in JUnit 5?Pass the test and this pattern moves unknown