Grouping test with JUnit categories

Junit 4.8 introduced a new feature called category, which provides a mechanism to group multiple test into a particular categories, thus giving an opportunity to include or exclude the specific category.

Junit category can be defines as a class or an interface. But since these act as a marker, defining it as an interface seems more convenient.


Example

The example below demonstrates the use of categories in Junit.

We will first create an interface that would serve as our category.

public interface SimpleTest{}

Next we will create our test classes.

public class TestClass1 {

    @Test
    public void testNormal1(){
	System.out.println("Testing Normal Test 1");
    }
	
    @Category(ComplexTest.class)
    @Test
    public void testComplex1(){
	System.out.println("Testing Complex Test 2");
    }
}
public class TestClass2 {
    @Test
    public void testSimple(){
	System.out.println("Testing Simple Test 2");
    }
	
    @Category(ComplexTest.class)
    @Test
    public void testComplex2(){
	System.out.println("Testing Complex Test 2");
    }
}

To execute a JUnit category, we need to create a Suite as shown below:

@RunWith(Categories.class)
@ExcludeCategory(ComplexTest.class)
@SuiteClasses({
	TestClass1.class,
	TestClass2.class
})
public class CategoryTestSuite {
	
}

Categories is a special test runner provided by Junit, which is used to run Junit categories. We use the annotation @ExcludeCategory. This would exclude the test method annotated with @Category (ComplexTest.class).

Below is the output from the CategoryTestSuite

Testing Normal Test 1
Testing Simple Test 2

We can also use @IncludeCategory annotaton in place of @ExcludeCategory, which would basically include only those tests that are annotated with @Category(ComplexTest.class).

@IncludeCategory(ComplexTest.class) would produce the following output:

Testing Complex Test 1
Testing Complex Test 2
RELATED ARTICLES

Testing Exception and Timeout with JUnit

Apart from testing positive scenarios, JUnit can also be used to test negative scenarios. It can be used to test scenarios where an exception is expected or a timeout is to be checked.

View Article

Unit Testing with JUnit

JUnit is a popular unit testing framework that helps developers to unit test their source code during the development of a java application. This articles demonstrates a basic JUnit Test Case.

View Article

Testing with Multiple Inputs using Parameterized Runner

JUnit Parameterized Test Runners provides the abiltiy to run the same test over and over again using different set of inputs.

View Article

Introducing JUnit Rule

Junit Rules allows developers to add additional functionalities that can applied to all test methods in a test class. It is similar to the concept of custom test runners but with reduced restrictions.

View Article

Understanding JUnit Test Suite

A JUnit Test Suite allows developers to bundle multiple test classes together into a single unit (called as test suite) and run them all together at once.

View Article