I want to run only a subset of my unit tests, which are defined by a specific @Category .
So, I read a few SO questions, such as this one (which is what I am looking for), as well as this one .
The solution to my problem is apparently provided by the ClasspathSuite project . So I started writing NewTest and OldTest interfaces that will define my test categories. Then I created the AllTests package:
@RunWith(ClasspathSuite.class) public class AllTests { }
After that, I created the AllNewTests package:
@RunWith(Categories.class) @IncludeCategory(NewTest.class) @SuiteClasses( { AllTests.class }) public class AllNewTests { }
Finally, I create two JUnit classes, one for each category:
@Category(NewTest.class) public class SomeNewTests {
Now, if I run AllTests , Eclipse runs all the tests of my project, while Maven does not work, because no tests were found:
mvn test -Dtest=AllTests ... ------------------------------------------------------- TESTS ------------------------------------------------------- Running my.company.AllTests Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.093 sec There are no tests to run.
If I run AllNewTests (what should I do, right?), Everything is fine in Eclipse (i.e. it runs tests written using @Category(NewTest.class) ), but Maven returns an error :
mvn test -Dtest=AllNewTests ... ------------------------------------------------------- TESTS ------------------------------------------------------- Running my.company.AllNewTests Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.125 sec <<< FAILURE! Results : Tests in error: initializationError(my.company.AllNewTests) Tests run: 1, Failures: 0, Errors: 1, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------
The exception is the following:
org.junit.runner.manipulation.NoTestsRemainException at org.junit.runners.ParentRunner.filter(ParentRunner.java:256) at org.junit.experimental.categories.Categories.<init>(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)
My question is what did I do wrong?
Technical Details: Java 6, Maven 3.0.2, JUnit 4.8.1, Surefire 2.7.1 Plugin, cpsuite-1.2.5