@RunWith(Suite.class) @Suite.SuiteClasses({ SomeTests.class, SomeOtherTests.class, SomethingElseTests.class }) public class SomeTestSuite { // this class is just a place holder for test suite annotations above }
Next logical step would be aggregation of tests based on the information contained in tests themselves. Instead of specifying concrete tests, test suites would contain qualifiers (using annotations) to match. Those tests having matching qualifiers are included in a suite, those without are not. For example I have a qualifier JMSTest that is obviously assigned to tests that use a JMS provider.
Thus, my test suite classes will become completely decoupled from the tests and vice verse. This is actually even better than it sounds: even though there is no language dependency from tests to test suites in JUnit, functional dependency does exist: tests will not run if they are not bound to one or more test suites.
With introduction of JUnit Categories we received a qualifier support that is almost what we need:
public interface JMSTest {} @RunWith(Categories.class) @Categories.IncludeCategory(JMSTest.class) @Suite.SuiteClasses({ SomeTestSuite.class, }) public class JMSTestSuite {} @Category(IntegrationTest.class) public class SomeTests { .... } @Category(JMSTest.class) Public class SomeOtherTests{ .... } @Category(DatabaseTest.class) Public class SomethingElseTests{ .... }
This setup will run SomeOtherTests marked with JMSTest category when running JMSTestSuite. But categories didn’t eliminate the dependency from suites to tests – we still depend on test suite SomeTestSuite that explicitly references our tests.
Now, imagine you can define suite AllProjectTests that always contains all tests from the project. Then you can define category-based test suites like JMSTestSuite above and never care about maintaining your test suites again. Fortunately, we can use open source project to do just that - ClasspathSuite:
import org.junit.extensions.cpsuite.ClasspathSuite; import org.junit.runner.RunWith; @RunWith(ClasspathSuite.class) public class AllProjectTests {} @RunWith(Categories.class) @Categories.IncludeCategory(JMSTest.class) @Suite.SuiteClasses({ AllProjectTests.class, }) public class JMSTestSuite {}
To summarize, if you want to define a test suite that runs all database-based tests:
1. Define AllProjectTests suite using ClasspathSuite.
2. Define JUnit category DatabaseTest.
3. Define corresponding suite DatabaseTestSuite to run all tests marked with category DatabaseTest.
4. More complex category-based suites are easy to construct with JUnit category support.