Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

JUnit Test Suites

farenda 2016-01-04 0

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!
Categories JUnit Tags java, junit, unit-tests
Previous: JUnit Test Runners
Next: JUnit Parameterized tests

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored