Out of the box test categorization in JUnit 4
The newest version of JUnit does not contain a category annotation, like its rival TestNG or its distant cousin NUnit; however, this doesn’t mean you can’t easily categorize your tests. Much like pre-JUnit 4, the solution involves the use of suites; however, as anyone who is actually using JUnit 4 knows, suites, as we used to them, are history– they’ve been replaced with annotations.
Briefly, in JUnit 4, suites have been replaced by two annotations: @RunWith and @SuiteClasses. The @RunWith annotation requires it be set to the Suite class and then the @SuiteClasses annotation accepts a list of class to be run. It is this annotation that facilitates test categorization– for example, to run all unit tests, you would list them all as follows:
@RunWith(Suite.class)
@SuiteClasses({AccountEqualsTest.class, UserTest.class, CalcTest.class})
public class AllUnitTests {
}
Likewise, for component or system tests , you would create similar classes using the @SuiteClasses annotation. As you can see, traditional suite classes are gone in JUnit 4, but the same strategy for test categorization is essentially there– group like tests via suites.

March 3rd, 2008 at 7:31 am
Thank you for this. I’m a bit disappointed that JUnit 4 couldn’t mimic TestNG’s group feature…but at least there’s a work around. Thanks.
April 27th, 2009 at 6:15 am
You can build in JUnit4 categorizations at the test method level! Each test method is assigned to a set of categories and the test suite can choose to run tests of certain categories. You will need to add a custom runner to handle this suite. You can check for details here - http://testingcraft.blogspot.com/
November 25th, 2009 at 4:50 am
This is great but unfortunately doesn’t seem to work with Maven 2 very well. I’m using Cobertura and configuring surefire to report only (not re-run the tests) and unfortunately I just get two entries for the tests that the suite runs because those tests run twice! Anyone got any idea how to stop the tests running twice?