title: How do I Dynamically create a Test Suite in JUnit 4?
body: <p>I would like to create a junit test suite using JUnit 4 where the names of the test classes to be included are not known until the test suite is run.</p>
  
  <p>In JUnit 3 I could do this:</p>
  
  <pre><code>public final class MasterTester extends TestCase
  {
    /**
     * Used by junit to specify what TestCases to run.
     * 
     * @return a suite containing what TestCases to run
     */
    public static TestSuite suite() {
      TestSuite suite = new TestSuite();
  
      for(Class&lt;?&gt; klass : gatherTestClasses()) {
        suite.addTestSuite(klass);
      }
  
      return suite;
    }
  }
  </code></pre>
  
  <p>and let the <code>gatherTestClasses()</code> method deal with figuring out what test classes to run.</p>
  
  <p>In JUnit 4, the <a href="http://kentbeck.github.com/junit/javadoc/latest/" rel="noreferrer">documentation</a> says to use an annotation: <code>@SuiteClasses({TestClass1.class, TestClass2.class...})</code> to build up my test suite.  There are <a href="https://stackoverflow.com/questions/190224/junit-and-junit-framework-testsuite-no-runnable-methods/190347#190347">numerous SO answers</a> showing how to do this.  Unfortunately the examples I see do not seem to allow for passing a dynamically generated list of TestClasses.</p>
  
  <p>This <a href="https://stackoverflow.com/questions/1645154/how-do-i-define-a-testsuite-without-using-suiteclasses-in-junit-4-5">SO answer</a> suggested I would have to subclass <code>BlockJUnit4ClassRunner</code> which I do not want to do.</p>
  
  <p>Dynamically specified test suites seem like something that must be in JUnit 4 somewhere.  Does anyone know where?</p>