27

When we say mvn test, usual way is that maven will look for the tests present in src/test/java folder. But I have my tests in some different folder, namely src/integration-test/java. How do I run the tests present in this folder through command line?

Thanks in advance,

Manoj.

Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
user2649233
  • 912
  • 4
  • 14
  • 28

2 Answers2

35

First you shouldn't run those integration test via the test life cycle, cause pre-integration-test, integration-test and post-integration-test life cycle phase exist. Apart from that for integration tests the maven-failsafe-plugin is responsible.

There are several options to handle your situations. First you should follow the naming conventions for integration tests

<includes>
 <include>**/IT*.java</include>
 <include>**/*IT.java</include>
 <include>**/*ITCase.java</include>
</includes>

which means to put the integration tests into the default folder src/test/java. If you have a multi-module build it would be the best having a separate module which contains the integration-tests only or you can go the path you decided to use a separate folder (which is not the best):

First you need to add the folder using the buildhelper-maven-plugin to get those integration tests being compiled like this:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
      <execution>
        <id>add-test-source</id>
        <phase>process-resources</phase>
        <goals>
          <goal>add-test-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>src/integration-test/java</source>
          </sources>
        </configuration>
      </execution>
    </executions>
  </plugin>

and you have to configure the maven-failsafe-plugin like this:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.14.1</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

After you have configured you can run your integration tests via:

mvn verify
StvnBrkdll
  • 3,924
  • 1
  • 24
  • 31
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • just wondering, if there is nothing happening in post-integration-test, would it be equally valid to use `mvn integration-test` ? – vikingsteve May 28 '13 at 07:19
  • Yes that will work, but I would suggest to use always `mvn verify` which is after post-integration and makes sure to run post-integration phase. – khmarbaise May 28 '13 at 07:39
18

@khmarbaise is right with his recommendation (so +1 for that) but I want to answer your question without speculating about the reasons why the test source are located somewhere else.

If your tests are located in another directory than the standard src/test/java directory, the most simple solution is to change the default value of the testSourceDirectory configuration parameter which is defined in the Super POM.

e.g. for src/foobar/java use

<build>
  <testSourceDirectory>src/foobar/java</testSourceDirectory>
</build>

then you can simply run mvn test to execute the tests.


More complex solution...

If you do not want to change the pom.xml configuration you can specifiy the testSourceDirectory parameter on the command line like this:

mvn -DtestSourceDirectory=src/foobar/java clean test

But be sure that your sources are compiled. Otherwise they will not be found and executed. In the above example the test sources are not placed at a location that gets compiled by default, so we nevertheless have to change the pom and add the directory to the list of test sources by using the buildhelper plugin:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>add-test-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/foobar/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

If you do not want to change the configuration of the default value in the pom and not want to pass the new directory at the commandline you have to configure the path in the maven-buildhelper-plugin and the maven-surefire-plugin in your pom.xml like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/foobar/java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14.1</version>
            <configuration>
                <testSourceDirectory>src/foobar/java</testSourceDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

Now again the simple usage of mvn test will execute the test at the not standard location.

StvnBrkdll
  • 3,924
  • 1
  • 24
  • 31
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • As already mentioned in the question we are talking about integration tests and *not* about unit tests so to change the configuration for maven-surefire-plugin which is responsible for unit tests is the wrong way. Better use maven-failsafe-plugin which is intended for integration tests. Apart from that based on your suggested change the integration tests would run during the test life-cycle which is against the conventions of Maven. – khmarbaise May 28 '13 at 08:40
  • 1
    @khmarbaise As I already pointed out, I follow your recommendation and gave a +1. But the title of the question is _How to run **UnitTests** ..._ and the only hint to integration test is the name of the directory that is used for the test code (I can not find a place where @K S MANOJ points out that he is running integration test and that he talks about the failsafe plugin). So I think I gave a reasonable answer to the simple questions how to run tests in a different folder than the default one (_But i have my tests in some different folder..._). – FrVaBe May 28 '13 at 08:54
  • @khmarbaise By the way my solution does not affect the execution of the surefire or failsafe plugin (e.g. running in different life-cycle). Whatever naming convention @K S MANOJ choosed or whatever plugin configuration he did (we do not know...), changing the default folder for the test sources will not change any of these settings and the build will run as configured by @K S MANOJ. – FrVaBe May 28 '13 at 09:27