0
0
JunitHow-ToBeginner ยท 3 min read

How to Use Test Suite in JUnit: Syntax and Example

In JUnit, you use a test suite to group multiple test classes and run them together. You create a suite class annotated with @RunWith(Suite.class) and list the test classes inside @Suite.SuiteClasses. This helps organize and run related tests in one go.
๐Ÿ“

Syntax

A JUnit test suite uses the @RunWith(Suite.class) annotation to specify it is a suite. The @Suite.SuiteClasses annotation lists the test classes to include in the suite. Each class inside this list should be a valid JUnit test class.

  • @RunWith(Suite.class): Tells JUnit to run this class as a suite.
  • @Suite.SuiteClasses({TestClass1.class, TestClass2.class}): Specifies which test classes to run.
  • The suite class itself has no test methods.
java
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    TestClass1.class,
    TestClass2.class
})
public class MyTestSuite {
    // no test methods here
}
๐Ÿ’ป

Example

This example shows two simple test classes and a test suite that runs both. Running the suite will execute all tests from both classes.

java
import org.junit.Test;
import static org.junit.Assert.*;

public class TestClass1 {
    @Test
    public void testAddition() {
        assertEquals(5, 2 + 3);
    }
}

public class TestClass2 {
    @Test
    public void testSubtraction() {
        assertEquals(2, 5 - 3);
    }
}

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    TestClass1.class,
    TestClass2.class
})
public class MyTestSuite {
    // no test methods
}
Output
Test run finished after 50 ms [ 2 tests completed successfully ]
โš ๏ธ

Common Pitfalls

  • Forgetting to annotate the suite class with @RunWith(Suite.class) causes it not to run as a suite.
  • Not listing all test classes inside @Suite.SuiteClasses means those tests won't run.
  • Including classes that are not test classes will cause errors.
  • Suite classes should not contain test methods themselves.
java
/* Wrong: Missing @RunWith annotation */
public class WrongSuite {
    // This will not run as a suite
}

/* Right: Correct suite setup */
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class})
public class CorrectSuite {
}
๐Ÿ“Š

Quick Reference

Use this quick reference to create a JUnit test suite:

AnnotationPurpose
@RunWith(Suite.class)Marks the class as a test suite runner
@Suite.SuiteClasses({Class1.class, Class2.class})Lists test classes to run in the suite
Test classesContain actual test methods annotated with @Test
Suite classContains no test methods, only annotations
โœ…

Key Takeaways

Use @RunWith(Suite.class) and @Suite.SuiteClasses to create a JUnit test suite.
List all test classes you want to run together inside @Suite.SuiteClasses.
Suite classes should not have test methods themselves.
Make sure all listed classes are valid JUnit test classes.
Running the suite runs all tests from the included classes in one go.