A test suite groups multiple test classes together so they can be executed as a single run, useful for organizing related tests or running a full regression pass.
Key Points: • In JUnit 4, create a suite class annotated with @RunWith(Suite.class) and @Suite.SuiteClasses({TestA.class, TestB.class}). • The suite class itself typically has no test methods - it just declares which classes to include. • In JUnit 5, suites are built with the junit-platform-suite module using @Suite and @SelectClasses or @SelectPackages. • Test suites are useful for grouping tests by feature area or for defining a smoke-test subset versus a full regression subset. • Most modern build tools (Maven Surefire/Failsafe, Gradle) can already run all tests in a module without a suite, making explicit suites less essential than they once were.
Example: A RegressionSuite class listing UserServiceTest, OrderServiceTest, and PaymentServiceTest lets you run all three together with a single test execution.
Code Example:
@RunWith(Suite.class)
@Suite.SuiteClasses({
UserServiceTest.class,
OrderServiceTest.class,
PaymentServiceTest.class
})
public class RegressionSuite {
}Interview Tip: A concise interview answer is:
"A test suite bundles several test classes so they run together as one unit. In JUnit 4 I'd annotate an empty class with @RunWith(Suite.class) and @Suite.SuiteClasses listing the test classes; in JUnit 5 the junit-platform-suite module provides @Suite with @SelectClasses instead."