Jenkins : Using Testability Explorer in Maven 2 projects

Explains a temporary solution how to use the Testability Explorer in Maven 2 projects. 

Background

Testability Explorer is an open-source tool that identifies hard-to-test Java code. Right now, you can run the program from the Command Line or using the Ant Task. There is no Maven 2 plugin available at the moment. You have to use the maven-antrun-plugin for the time being. However, there is one big problem. The Testability Explorer JAR files are not available yet in any Maven 2 repository. This page offers a temporary solution for this problem until the JAR files become available in any public Maven 2 repository.

Installing the JAR files to local Maven repo

In order to use the maven-antrun-plugin with the com.google.ant.TestabilityTask, you have to add two dependencies into your Maven 2 pom.xml under the dependencies section.

<dependency>
    <groupId>com.google.code.testability-explorer</groupId>
    <artifactId>testability-explorer</artifactId>
    <version>1.3</version>
</dependency>

<dependency>
    <groupId>com.google.code.testability-explorer</groupId>
    <artifactId>ant-testability-explorer</artifactId>
    <version>1.3</version>
</dependency>

As said before, the JAR files are not available in any public repository. You have to add them manually into your local repository. Run the Maven install commands:

mvn install:install-file  -Dfile=[path to]/testability-explorer-1.3.0-r275.jar \
-DgroupId=com.google.code.testability-explorer -DartifactId=testability-explorer -Dversion=1.3 \
-Dpackaging=jar -DcreateChecksum=true

mvn install:install-file  -Dfile=[path to]/ant-testability-explorer-1.3.0-r275.jar \
-DgroupId=com.google.code.testability-explorer -DartifactId=ant-testability-explorer -Dversion=1.3 \
-Dpackaging=jar -DcreateChecksum=true

You are now ready to use the Testability Explorer in a Maven 2 powered project locally.

Running the Ant Task from Maven

Now that you have the JAR files available locally, you can use the maven-antrun-plugin to run the Testability Explorer during builds. Go to the build section of your pom.xml and add the following plugin (if you do not have a build section in your pom.xml, you have to create <build> and <plugins> first).

<build>
    <plugins>
        ...

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>Testability Explorer</id>
                    <phase>test</phase>
                    <configuration>
                        <tasks>
                            <path id="ant.classpath">
                                <path refid="maven.runtime.classpath" />
                            </path>

                            <echo message="Running Testability Explorer" />

                            <property name="testability.report.dir" value="target/reports/testability" />
                            <delete dir="${testability.report.dir}" />
                            <mkdir dir="${testability.report.dir}" />

                            <taskdef name="testability" classname="com.google.ant.TestabilityTask">
                                <classpath refid="maven.runtime.classpath" />
                            </taskdef>
                            <testability filter="my.package" resultfile="${testability.report.dir}/results.xml" print="xml">
                                <classpath refid="maven.runtime.classpath" />
                            </testability>

                            <echo message="Done. Reports can be found in ${testability.report.dir}/results.xml" />
                        </tasks>

                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

This will execute the com.google.ant.TestabilityTask during the test phase of a Maven 2 run, ie. if you run mvn test. Note: in the XML snippet above you have to replace my.package with your own Java package that you want to have tested.

Prospects

The suggested solution is far from optimal. As the JAR files will become available in a public Maven 2 repository, you will not have to copy them manually into you local repository. Sooner or later, there will also be am official Maven 2 plugin, so that the maven-antrun-plugin will not be required anymore.

Attachments:

m2-testability-jars.zip (application/zip)