Java Programming Tutorials

Java programming tutorials with many code examples!

JUnit Test Suites

JUnit Test Suite is a powerful feature to group a bunch of related tests in one place for easy execution. In this post we’re going to show how to use Test Suites in practice.

In this example we’ve got two test classes for hypothetical calculator. The first one is bunch of JUnit tests for addition:

package com.farenda.junit;

import org.junit.Test;

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

public class AdditionTest {

    @Test
    public void shouldAddNumbers() {
        assertThat(2+2, is(4));
    }
}

And the second test class is a group of comprehensive tests for substraction:

package com.farenda.junit;

import org.junit.Test;

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

public class SubstractionTest {

    @Test
    public void shouldSubstractNumbers() {
        assertThat(42-2, is(40));
    }
}

In both test classes we use CoreMatchers, which you already know from one of the previous posts.

To collect the above tests in one, executable group of tests we’re going to use Test Runner feature to execute our JUnit tests using Suite test runner. To work correctly Suite Runner needs to know of which test classes it consists of. This information is provided using @Suite.SuiteClasses annotation as in the above example:

package com.farenda.junit;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
        AdditionTest.class,
        SubstractionTest.class
})
public class CalculatorTestSuite {

    // Content may be empty.
    // Only annotations matter.
}

Now, we you execute the above suite you will see that all contained test classes have been executed:

JUnit Test Suite execution

JUnit Test Suite execution

How pros do it

In sources of Spring Framework one can see many applications of JUnit Test Suites. Here’s sample AnnotationConfigTestSuite:

package org.springframework.test.context.junit4.annotation;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

/**
 * JUnit test suite for annotation-driven <em>configuration class</em>
 * support in the Spring TestContext Framework.
 *
 * @author Sam Brannen
 * @since 3.1
 */
@RunWith(Suite.class)
// Note: the following 'multi-line' layout is for enhanced code readability.
@SuiteClasses({//
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
      DefaultConfigClassesBaseTests.class,//
      DefaultConfigClassesInheritedTests.class,//
      BeanOverridingDefaultConfigClassesInheritedTests.class,//
      ExplicitConfigClassesBaseTests.class,//
      ExplicitConfigClassesInheritedTests.class,//
      BeanOverridingExplicitConfigClassesInheritedTests.class,//
      DefaultLoaderDefaultConfigClassesBaseTests.class,//
      DefaultLoaderDefaultConfigClassesInheritedTests.class,//
      DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests.class,//
      DefaultLoaderExplicitConfigClassesBaseTests.class,//
      DefaultLoaderExplicitConfigClassesInheritedTests.class,//
      DefaultLoaderBeanOverridingExplicitConfigClassesInheritedTests.class //
})
public class AnnotationConfigTestSuite {
}
Share with the World!