Exclude maven dependency for one specific unit test

java, junit, maven, unit-testing

Solution

Not by using one Surefire execution.

You will have to define two executions of the Surefire plugin: one containing the full Classpath for most of the tests, and one containing the specialized Classpath for the single test that requires it.

Follow the Surefire plugin's documentation: http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

You'll have to create two executions, and bind them both to the `test` phase. Use the following example as a skeleton (you'll have to adjust the `include` and `exclude` patterns, as well as the excluded Classpath artifact):

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <executions>
            <execution>
                <id>full-cp</id>
                <phase>test</phase>
                <goals>
                    <goal>test</goal>
                </goals>
                <configuration>
                    <includes>
                        <include>**/Test*.java</include>
                    </includes>
                    <excludes>
                        <exclude>MyFancyTest.java</exclude>
                    </excludes>
                </configuration>
            </execution>
            <execution>
                <id>special-cp</id>
                <phase>test</phase>
                <goals>
                    <goal>test</goal>
                </goals>
                <configuration>
                    <includes>
                        <include>MyFancyTest.java</include>
                    </includes>
                    <classpathDependencyExcludes>
                        <classpathDependencyExcludes>excluded-artifact</classpathDependencyExcludes>
                    </classpathDependencyExcludes>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

Problem

I'd like to remove a dependency for a unit test. I found how to do it in this answer. But I'd like to remove a dependency for only one specific test, not for all my tests. Is there a way to do that?

Original source

Related problems