junit

JUnit Categories Example

1. Introduction

JUnit has an awesome feature of organizing group of test cases called Categorizing. It can help developers differentiate test cases from one another. In this post, I’ll showcase how easy it is to categorize unit tests by @Category.
 
 
 
 
 
 

2. Maven Project and Configuration

pom.xml

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jgc.areyes.junit</groupId>
    <artifactId>junit-categories-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.17</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

3. Source code sample

Define the interfaces first. In order for us to group test cases, we need to create a unifier/union on them. We use interface class to tag a specific class or method to a group. Here are the interfaces that we will use in this example.

Interfaces

1
2
3
4
public interface FunctionalGroupTests1 {}
public interface FunctionalGroupTests2 {}
public interface IntegrationTests {}
public interface SanityTests {}

We then use those interfaces on our test cases. This will differentiate the test case for our own test purposes. In the example below, we tag tests method by category using the @Category annotation

JUnitTestCategoryExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.areyes.junit.svc;
 
import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.*;
 
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
 
import com.areyes.junit.cat.intf.FunctionalGroupTests1;
import com.areyes.junit.cat.intf.FunctionalGroupTests2;
import com.areyes.junit.cat.intf.IntegrationTests;
import com.areyes.junit.cat.intf.SanityTests;
 
public class JUnitTestCategoryExample {
 
    @Test @Category(FunctionalGroupTests1.class)
    public void testFunctionalTests1Test1() {
        //  You're test case here: Below is just an example.
        int numberInLoop = 0;
        for (int i=0;i<1000;i++) {
            numberInLoop++;
             
        }
        System.out.println("FunctionalGroupTests1: testFunctionalTests1Test1");
        Assert.assertThat(numberInLoop,isA(Integer.class));
    }
     
    @Test @Category(FunctionalGroupTests1.class)
    public void testFunctionalTests1Test2() {
        //  You're test case here: Below is just an example.
        int numberInLoop = 0;
        for (int i=1000;i<4000;i++) {
            numberInLoop++;
             
        }
        System.out.println("FunctionalGroupTests1: testFunctionalTests1Test2");
        Assert.assertThat(numberInLoop,isA(Integer.class));
    }
     
    @Test @Category(FunctionalGroupTests2.class)
    public void testFunctionalTests2Test1() {
        //  You're test case here: Below is just an example.
        int numberInLoop = 0;
        do{
            numberInLoop++;
        }while(numberInLoop != 1000);
        System.out.println("FunctionalGroupTests2: testFunctionalTests2Test1");
        Assert.assertThat(numberInLoop,isA(Integer.class));
    }
     
    @Test @Category(FunctionalGroupTests2.class)
    public void testFunctionalTests2Test2() {
        System.out.println("FunctionalGroupTests2: testFunctionalTests2Test2");
    }
 
    @Test @Category({IntegrationTests.class,FunctionalGroupTests1.class})
    public void testIntegrationTestsTest1() {
        System.out.println("IntegrationTests: testIntegrationTestsTest1");
    }
     
    @Test @Category(SanityTests.class)
    public void testSanityTestsTest1() {
        System.out.println("SanityTests: testSanityTestsTest1");
    }  
}

4. Running our example

4.1 Running Tests by Category

We can run specific test case category by running the commands in Maven below. mvn test -Dgroups="com.areyes.junit.cat.intf.FunctionalGroupTests1, com.areyes.junit.cat.intf.FunctionalGroupTests2"
mvn test -Dgroups="com.areyes.junit.cat.intf.IntegrationTests, com.areyes.junit.cat.intf.SanityTests"

Want to be a JUnit Master ?
Subscribe to our newsletter and download the JUnit Programming Cookbook right now!
In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!

4.2 Running tests by Category Profile

Alternatively, we can run tests by profile. We need to update our pom.xml and add a new profiles. We will then use these profiles and tag the categories we created to each as shown below.

pom.xml

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jgc.areyes.junit</groupId>
    <artifactId>junit-categories-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.17</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <groups>${testcase.groups}</groups>
                    <excludes>
                        <exclude>${exclude.tests}</exclude>
                    </excludes>
                    <includes>
                        <include>${include.tests}</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>sanityTests</id>
            <properties>
                <testcase.groups>com.areyes.junit.svc.SanityTests</testcase.groups>
            </properties>
        </profile>
        <profile>
            <id>functionalGroupTests1</id>
            <properties>
                <testcase.groups>com.areyes.junit.svc.FunctionalGroupTests1</testcase.groups>
            </properties>
        </profile>
        <profile>
            <id>functionalGroupTests2</id>
            <properties>
                <testcase.groups>com.areyes.junit.svc.FunctionalGroupTests2</testcase.groups>
            </properties>
        </profile>
        <profile>
            <id>integrationTests</id>
            <properties>
                <testcase.groups>com.areyes.junit.svc.IntegrationTests</testcase.groups>
            </properties>
        </profile>
    </profiles>
     
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Run them by using the following maven command: mvn test -pfunctionalGroupTests1

5. Download the Eclipse project

This was an example of JUnit Category.

Download
You can download the full source code of this example here: junit-categories-example
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.
Subscribe
Notify of
guest


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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button