30

I have a jar in my maven repository that contains junit tests, which should be run in different projects, because it is able to inspect the project and test for certain features of it. Unforunately surefire doesn't pick up tests that are contained in a jar, as this Feature Request shows.

In the feature request they propose to unpack the jar to be then executed by surefire.

I successfully unpacked the jar using the maven-dependency-plugin, but the contained tests are not executed anyway. This is how I configured the maven-dependency-plugin to unpack my jar:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>process-test-classes</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>de.mwx.test</groupId>
                        <artifactId>selenium-test-base</artifactId>
                        <version>0.1</version>
                        <overWrite>true</overWrite>
                          <outputDirectory>
                              ${project.build.directory}/classes
                          </outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

Any help would be appriciated.

devsnd
  • 7,382
  • 3
  • 42
  • 50
  • 1
    Why are the tests in another jar? Are they tests for your project, or another? If another, why are you running them? If for your app, why aren't they in your project? – Dave Newton May 08 '12 at 11:03
  • The other jar is actually a test framework that operates on the current project and executes miscellaneous tasks. It seemed to me like the most modular way: But it wasn't the intended use for surefix, I guess. – devsnd May 08 '12 at 11:36
  • 1
    But maven projects already define a standard for a project's tests. And IMO it doesn't make sense to make a project dependent on its tests--if anything, tests would be dependent on the module under test, since tests use the module, but not vice-versa. – Dave Newton May 08 '12 at 12:39
  • 2
    It does make sense to make a project to be dependent of its tests if the project itself is a test. the depending jar is a framework that calls the tests in the current project. This makes it easy to configure a fleet of tests on the fly. – devsnd May 08 '12 at 13:11
  • 1
    IMO, no, that doesn't make sense. – Dave Newton May 08 '12 at 13:11
  • If this worked, you could use this kind approach to add a dependency for Consumer driven contracts : http://martinfowler.com/articles/consumerDrivenContracts.html – plasma147 Feb 28 '14 at 22:18

3 Answers3

46

There is a way of running a test in maven from another jar. from maven-surefire-plugin version 2.15 you can tell maven to scan your test jars for tests and run them. You don't need to extract the tests jar. Just add a dependency to your test jar and:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <dependenciesToScan>
                <dependency>test.jar.group:test.jar.artifact.id</dependency>
            </dependenciesToScan>
        </configuration>
    </plugin>

Took this stuff from https://gist.github.com/aslakknutsen/4520226 And https://issues.apache.org/jira/browse/SUREFIRE-569

As expected, this works for JUnit and Testng. Will probably work for anything that surefire can run.

galusben
  • 5,948
  • 6
  • 33
  • 52
3

(This is just restating what is in a comment above from khmarbaise, but since it wasn't clarified, I think it's worth restating):

Use the test-classes directory instead of the classes folder as outputDirectory:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>process-test-classes</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>de.mwx.test</groupId>
                        <artifactId>selenium-test-base</artifactId>
                        <version>0.1</version>
                        <overWrite>true</overWrite>
                          <outputDirectory>
                              ${project.build.directory}/test-classes
                          </outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
Eric Pabst
  • 536
  • 4
  • 12
1

As described in the issue you need to have a Suite which is contains in your project which is NOT located in the test jar.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Yes, thats what the project I'm talking about is. I will update my question to make that point more clear. – devsnd May 08 '12 at 11:37
  • 1
    I would suggest to use the test-classes directory instead of the classes folder as outputDirectory. – khmarbaise May 08 '12 at 18:46