5

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?

Community
  • 1
  • 1
FBB
  • 1,414
  • 2
  • 17
  • 29
  • 1
    I don't think so, cause dependencies are for the maven module. If you need this for only a single test you might need to move this test to a separate maven module. But it sounds like integration tests and not like a unit test. May be you can elaborate a little bit more or post your pom file? – khmarbaise Apr 15 '13 at 15:56
  • It's true that it is more an integration test, it is to test the behavior of my application when a specific dependency is missing. – FBB Apr 15 '13 at 15:59
  • 1
    Than you should separate out that test into a separate maven module. – khmarbaise Apr 15 '13 at 16:04
  • Then you need to create one module for each integration test?! – FBB Apr 15 '13 at 18:32

1 Answers1

3

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>
Isaac
  • 16,458
  • 5
  • 57
  • 81
  • 1
    Surefire plugin is not intended for integration tests. Integration test should be run by the maven-failsafe-plugin instead. – khmarbaise Apr 15 '13 at 16:05
  • Thx a lot. While khmarbaise is right, Isaac answers my question nevertheless. I will look into maven-failsafe-plugin. But the documentation looks terrible :p – FBB Apr 15 '13 at 18:29