title: Running all tests from a @Category using Maven
body: <p>I want to run only a subset of my unit tests, the ones defined by a specific <code>@Category</code>.</p>
  
  <p>So I read several SO questions, such as <a href="https://stackoverflow.com/questions/3100924/how-to-run-junit-tests-by-category-in-maven">this one</a> (which is exactly what I am looking for), and also <a href="https://stackoverflow.com/questions/2176570/how-to-run-all-tests-belonging-to-a-certain-category-in-junit-4">this one</a>.</p>
  
  <p>The solution of my problem seems to be provided by the <a href="http://www.johanneslink.net/projects/cpsuite.jsp" rel="nofollow noreferrer">ClasspathSuite project</a>. So I started to write the <code>NewTest</code> and <code>OldTest</code> interfaces that will define my test categories. Then, I created the <code>AllTests</code> suite:</p>
  
  <pre><code>@RunWith(ClasspathSuite.class)
  public class AllTests { }
  </code></pre>
  
  <p>After that, I created a <code>AllNewTests</code> suite:</p>
  
  <pre><code>@RunWith(Categories.class)
  @IncludeCategory(NewTest.class)
  @SuiteClasses( { AllTests.class })
  public class AllNewTests { }
  </code></pre>
  
  <p>Finally, I create two JUnit classes, one per category:</p>
  
  <pre><code>@Category(NewTest.class)
  public class SomeNewTests {
      // some tests...
  }
  
  @Category(OldTest.class)
  public class SomeOldTests {
      // some tests...
  }
  </code></pre>
  
  <hr/>
  
  <p>Now, if I run <strong>AllTests</strong>, Eclipse launches all the tests of my project, while Maven fails as no test are found:</p>
  
  <pre><code>mvn test -Dtest=AllTests
  
  ...
  -------------------------------------------------------
   T E S T S
  -------------------------------------------------------
  Running my.company.AllTests
  Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.093 sec
  There are no tests to run.
  </code></pre>
  
  <p>If I run <strong>AllNewTests</strong> (which is the correct thing to do, right?), in Eclipse everything is fine (i.e. it only run the tests annoted with <code>@Category(NewTest.class)</code>) but <strong>Maven returns an error</strong>:</p>
  
  <pre><code>mvn test -Dtest=AllNewTests
  
  ...
  -------------------------------------------------------
   T E S T S
  -------------------------------------------------------
  Running my.company.AllNewTests
  Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.125 sec &lt;&lt;&lt; FAILURE!
  
  Results :
  
  Tests in error:
    initializationError(my.company.AllNewTests)
  
  Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
  
  [INFO] ------------------------------------------------------------------------
  [INFO] BUILD FAILURE
  [INFO] ------------------------------------------------------------------------
  </code></pre>
  
  <p>The exception thrown is the following:</p>
  
  <pre><code>org.junit.runner.manipulation.NoTestsRemainException
      at org.junit.runners.ParentRunner.filter(ParentRunner.java:256)
      at org.junit.experimental.categories.Categories.&lt;init&gt;(Categories.java:142)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:35)
      at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
      at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
      at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
      at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
      at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
      at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:33)
      at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:146)
      at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
      at $Proxy0.invoke(Unknown Source)
      at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145)
      at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:70)
      at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
  </code></pre>
  
  <hr/>
  
  <p><strong>My question is what I did wrong?</strong></p>
  
  <hr/>
  
  <p>Technical details: Java 6, Maven 3.0.2, JUnit 4.8.1, Surefire plugin 2.7.1, cpsuite-1.2.5</p>