How To Group Multiple Test Classes into a Suite in JUnit 4.4?

Q

How To Group Multiple Test Classes into a Suite in JUnit 4.4?

✍: FYICenter.com QA Team

A

JUnit 4.4 stops using the "public static Test suite()" method to build a test suite class. It is now provides the org.junit.runners.Suite class and two annotations to help you to build test suite.

org.junit.runners.Suite - JUnit 4.4 runner class that runs a group of test classes.

org.junit.runner.RunWith - JUnit 4.4 class annotation that specify runner class to run the annotated class.

org.junit.runner.Suite.SuiteClasses - JUnit 4.4 class annotation that specify an array of test classes for the Suite.class to run.

The annotated class should be an empty class.

To run "@Suite.SuiteClasses" class, you can use the core runner: org.junit.runner.JUnitCore.

Here is a good example of "@Suite.SuiteClasses" class:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
// by FYICenter.com

// specify a runner class: Suite.class
@RunWith(Suite.class)

// specify an array of test classes
@Suite.SuiteClasses({
  HelloTest.class, 
  ExpectedExceptionTest1.class,
  ExpectedExceptionTest2.class,
  UnexpectedExceptionTest1.class,
  UnexpectedExceptionTest2.class}
)

// the actual class is empty
public class AllTests {
}

2008-01-29, 4588👍, 0💬