JUnit – Categories Test

In JUnit, you can organize the test cases into different categories, and run those categorized test cases with @Categories.ExcludeCategory or @Categories.IncludeCategory

Note
This @Categories annotation is available since JUnit 4.12

1. Category = Marker Interface

In JUnit, you need to create marker interfaces to represent the categories:

PerformanceTests.java

package com.mkyong.category;

//category marker interface
public interface PerformanceTests {
}
RegressionTests.java

package com.mkyong.category;

public interface RegressionTests {
}

2. @Category Examples

Organizing the test cases into different categories.

2.1 @Category on method level.

ClassA.java

package com.mkyong.category;

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

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class ClassA {

    @Category(PerformanceTests.class)
    @Test
    public void test_a_1() {
        assertThat(1 == 1, is(true));
    }

    @Test
    public void test_a_2() {
        assertThat(1 == 1, is(true));
    }

}

2.2 @Category on class level.

ClassB.java

package com.mkyong.category;

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

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@Category({PerformanceTests.class, RegressionTests.class})
public class ClassB {

    @Test
    public void test_b_1() {
        assertThat(1 == 1, is(true));
    }

}

2.3 Multiple @Category examples.

ClassC.java

package com.mkyong.category;

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

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class ClassC {

    @Category({PerformanceTests.class, RegressionTests.class})
    @Test
    public void test_c_1() {
        assertThat(1 == 1, is(true));
    }

    @Category(RegressionTests.class)
    @Test
    public void test_c_2() {
        assertThat(1 == 1, is(true));
    }

}

3. Suite Test

Examples to run the categorized test cases.

3.1 Include category example, run PerformanceTests category.

PerformanceTestSuite.java

package com.mkyong.category;

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

@RunWith(Categories.class)
@Categories.IncludeCategory(PerformanceTests.class)
//Include multiple categories
//@Categories.IncludeCategory({PerformanceTests.class, RegressionTests.class})
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class PerformanceTestSuite {
}

Output

ClassA.test_a_1()
ClassB.test_b_1()
ClassC.test_c_1()

3.2 Include category example, run RegressionTestSuite category.

RegressionTestSuite.java

package com.mkyong.category;

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

@RunWith(Categories.class)
@Categories.IncludeCategory(RegressionTests.class)
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class RegressionTestSuite {
}

Output

ClassB.test_b_1()
ClassC.test_c_1()
ClassC.test_c_2()

3.3 Exclude category example.

ExcludePerformanceTestSuite.java

package com.mkyong.category;

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

@RunWith(Categories.class)
@Categories.ExcludeCategory(PerformanceTests.class)
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class ExcludePerformanceTestSuite {
}

Output

ClassA.test_a_2()
ClassC.test_c_2()
Note
This is similar with the TestNG group test.

References

  1. JUnit Categories JavaDoc
  2. JUnit Wiki – Categories
  3. TestNG Groups Test

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
1 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
Nandan SubramanianCaseyNaxos84KSLharis Recent comment authors
newest oldest most voted
Casey
Guest
Casey

Worked great, thanks!

Nandan Subramanian
Guest
Nandan Subramanian

Hi Mykong. You have a mistake in section 3 for sub section 3.1. The output for classC -> test_c_2 is not visible even though it is marked with the category RegressionTests.class. Can that be correct?

KSL
Guest
KSL

How do you select which category of tests to run?

Naxos84
Guest
Naxos84

See RegressionTestSuite.java and ExcludePerformanceTestSuite.java

But I like to know: Is this possible with maven?

haris
Guest
haris

this is very cool. thanks!

trackback
JUnit Tutorial

[…] Test – What is Mocking? and Why?JUnit + Spring integration exampleJUnit – Categories TestJUnit – Assert if a property exists in a classJUnit – Run test in a particular […]