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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <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
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
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"
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <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.
You can download the full source code of this example here: junit-categories-example