{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

Release of JUnit-DynamicSuite (0.2.0) (A Suite to filter JUnit 4.x Tests dynamically)

Did you ever have the problem that you only wanted to run certain unit tests from a directory in JUnit?

For example you might want to run fast running Tests only or skip integration tests ending with "ITCase".
Some IDEs do have good support for this Use-Case some don't...

Since I found this to be a common problem for some of my clients I created the JUnit-DynamicSuite.
This library allows you to filter tests from a Directory and / or classpath in a custom test suite:


After including the library into your unit test classpath you annotate any class with
to scan a directory for .class files or
@ClassPath(includeJars = true)
to scan the classPath (includeJars allows you to select wether jars should be included in the scan - false is default).

The @Filter annotation allows you to select a class which implements the TestClassFilter.
By implementing the TestClassFilter you can programmatically include or exclude any class found in the scope (directory / classpath) into the dynamic suite.
The implementation of the interface is as follows:
public interface TestClassFilter {

     * Filters Classes (by Name) found before they get loaded by the ClassLoader
     * @param className name of the class including its package
     * @return true if the class should be included in the TestSuite, false otherwise
    boolean include(String className);

     *  classes which are found by the ClassLookup algorithm are
     *  passed to this method for further filtering
     * @param cls classes which has been found by the class lookup Mechanism
     * @return true if it is a test class that should be included in the test suite, false otherwise
    boolean include(Class cls);


Imagine you have a scenario where you want to run all integration Tests from the "target/test-classes" directory which end with "ITCase" but are not slow (annotated with @Slow).
With the following code snippet you can achieve the described scenario:
package org.junit.extensions.dynamicsuite;

import org.junit.extensions.dynamicsuite.suite.DynamicSuite;
import org.junit.runner.RunWith;

public class IntegrationSuite implements TestClassFilter {

    public boolean include(String className) {
        //Only include classes ending with ITCase
        return className.endsWith("ITCase");

    public boolean include(Class cls) {
        //Exclude the slow integration tests
        return cls.getAnnotation(Slow.class) == null;

As you can see you can directly use the suite as filter by letting it implement the TestClassFilter interface and specifying it in @Filter.

Maven Dependency (from Maven Central)

This library has been uploaded to the Maven Central Repository and can be included with the following snippet.


Dynamically adding Junit tests to test suite should be fairly easy with this library and I hope it will be a useful addition to your unit testing toolbox.
Feedback is (of course) always appreciated!
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks