JUnit Categories – All Tactics with Examples!

JUnit Categories allow you to group tests together so we can run different categories of the test in different test suites. For example, we can separate slow tests from fast ones.

We can include or exclude categories with specified annotations. To assign a test case or a method to one of those categories the @Category annotation is provided. I tried to show how to use categories based on official Junit release notes.

First, we have to define our categories. FastTests and SlowTests.

FastTests.java

public interface FastTests { /* category marker */ }

SlowTests.java

public interface SlowTests { /* category marker */ }

A category can be either class or an interface. In the below code, we marked method b() of class A with @category annotation. Thus, we indicate that method b() belongs to the category of SlowTests. In this way, we marked not only the whole class, but also marked the test method individually.

A.java:

import org.junit.Test;
import org.junit.experimental.categories.Category;

public class A {
    @Test
    public void a() {
        System.out.println("a() method of class A has been run...\n");
    }

    @Category(SlowTests.class)
    @Test
    public void b() {
        System.out.println("b() method of class A has been run...\n");
    }
}

In the below code, class B is annotated with @Category annotation. Thus, this test class’s all test methods are included in this category. We see that a test class or a test method can be included in more than one category.

B.java:

import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category({SlowTests.class, FastTests.class})
public class B {
    @Test
    public void c() {
        System.out.println("c() method of class B has been run...\n");
    }
}

In the below code, you can see that our test suite name is SlowTestFirstSuite. Categories are also a kind of test suite. @IncludeCategory annotation indicates which categories will be included in execution. For the below example, we included SlowTest category and it will be executed. Thus, test method b() of class A, method c() of class B will be executed.

SlowTestFirstSuite.java

SlowTestFirstSuite.java
import org.junit.experimental.categories.Categories;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Categories.class)
@Categories.IncludeCategory(SlowTests.class)
@Suite.SuiteClasses({ A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestsFirstSuite {
//    b() method of class A has been run...
//    c() method of class B has been run...
}

In the below code, we annotated @ExcludeCategory that indicates which categories will be excluded from the execution. For the below code sample, test method b() of class A will be executed but method a() of class A and method c() of class B will not run because they are excluded from the execution by @ExcluedeCategory annotation. Test method a() of class A will not be executed for both cases because it is not in any category.

SlowTestSecondSuite.java

import org.junit.experimental.categories.Categories;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Categories.class)
@Categories.IncludeCategory(SlowTests.class)
@Categories.ExcludeCategory(FastTests.class)
@Suite.SuiteClasses({ A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestsSecondSuite {
    //b() method of class A has been run...
}

Github Project

https://github.com/swtestacademy/junit/tree/junit-categories

Thanks,
Onur Baskirt

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.