Christof has posted 1 posts at DZone. View Full User Profile

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

07.20.2011
| 3137 views |
  • submit to reddit

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:

Usage

After including the library into your unit test classpath you annotate any class with
 
@RunWith(DynamicSuite.class)
@Filter({}.class)
and
@Directory("bin")
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);
}

Examples

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;

@RunWith(DynamicSuite.class)
@Filter(IntegrationSuite.class)
@Directory("target/test-classes")
public class IntegrationSuite implements TestClassFilter {

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

    @Override
    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.
<dependency>
    <groupId>com.github.cschoell</groupId>
    <artifactId>junit-dynamicsuite</artifactId>
    <version>0.2.0</version>
    <scope>test</scope>
</dependency>

Conclusion


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!
0
Average: 4.5 (2 votes)
Published at DZone with permission of its author, Christof Schoell.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Harald Pehl replied on Tue, 2011/09/27 - 9:42am

I just came across your article. Well done. As soon as I have some time I will try to publish something about Piriti.

Christof Schoell replied on Tue, 2011/10/04 - 4:13pm in response to: Harald Pehl

Im looking forward to it :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.