views:

1465

answers:

4

Hello;

I have a maven2 multi-module project and in each of my child modules I have JUnit tests that are named *Test.java and *Integration.java for unit tests and integration tests respectively. When I exeucte:

mvn test

all of the JUnit tests *Test.java within the child modules are executed. When I execute

mvn test -Dtest=**/*Integration

none of the *Integration.java tests get execute within the child modules.

These seem like the exact same command to me but the one with the -Dtest=/*Integration** does not work it displays 0 tests being run at the parent level, which there are not any tests

+3  A: 

By default, Maven only runs tests that have Test somewhere in the class name.

Rename to IntegrationTest and it'll probably work.

Alternatively you can change the Maven config to include that file but it's probably easier and better just to name your tests SomethingTest.

From Inclusions and Exclusions of Tests:

By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

  • "*/Test.java" - includes all of its subdirectory and all java filenames that start with "Test".
  • "*/Test.java" - includes all of its subdirectory and all java filenames that end with "Test".
  • "*/TestCase.java" - includes all of its subdirectory and all java filenames that end with "TestCase".

If the test classes does not go with the naming convention, then configure Surefire Plugin and specify the tests you want to include.

cletus
Hi and thanksI have two kinds of tests normal POJO Junit tests called SomethingTest.java which get fired. I also have integration tests called SomethingIntegration.java which do not get fired. The SomethingTest.java get fired via mvn test or mvn install. The second tests do not get fired. mvn test -Dtest=**/*Integration
Peter Delaney
+4  A: 

You can set up maven's surefire to run unit tests and integration tests seperately. In the standard unit test phase you run everything that does not pattern match an integration test. You then create a second test phase that runs just the integration tests.

Here is an example

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>**/*IntegrationTest.java</exclude>
        </excludes>
      </configuration>
      <executions>
        <execution>
          <id>integration-test</id>
          <goals>
            <goal>test</goal>
          </goals>
          <phase>integration-test</phase>
          <configuration>
            <excludes>
              <exclude>none</exclude>
            </excludes>
            <includes>
              <include>**/*IntegrationTest.java</include>
            </includes>
          </configuration>
        </execution>
      </executions>
    </plugin>
serg10
I configured this as you said and only the *Test not the *Integration.java files will run when executing: mvn installI need to run my *Test.java as the default, but for my nightlty build I need to run both *Test.java and *Integration.java. I have to execute mvn install then cd to each sub-child directory and execute mvn -Dtest=**/*Integration test
Peter Delaney
Won't work -- see my answer.
HDave
A: 

You should try using maven failsafe plugin. You can tell it to include a certain set of tests.

James Kingsbery
A: 

I have done EXACTLY what you want to do and it works great. Unit tests "*Tests" always run, and "*IntegrationTests" only run when you do a mvn verify or mvn install. Here it the snippet from my POM. serg10 almost had it right....but not quite.

  <plugin>
    <!-- Separates the unit tests from the integration tests. -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
       <!-- Skip the default running of this plug-in (or everything is run twice...see below) -->
       <skip>true</skip>
       <!-- Show 100% of the lines from the stack trace (doesn't work) -->
       <trimStackTrace>false</trimStackTrace>
    </configuration>
    <executions>
       <execution>
          <id>unit-tests</id>
          <phase>test</phase>
          <goals>
             <goal>test</goal>
          </goals>
          <configuration>
                <!-- Never skip running the tests when the test phase is invoked -->
                <skip>false</skip>
             <includes>
                   <!-- Include unit tests within integration-test phase. -->
                <include>**/*Tests.java</include>
             </includes>
             <excludes>
               <!-- Exclude integration tests within (unit) test phase. -->
                <exclude>**/*IntegrationTests.java</exclude>
            </excludes>
          </configuration>
       </execution>
       <execution>
          <id>integration-tests</id>
          <phase>integration-test</phase>
          <goals>
             <goal>test</goal>
          </goals>
          <configuration>
            <!-- Never skip running the tests when the integration-test phase is invoked -->
             <skip>false</skip>
             <includes>
               <!-- Include integration tests within integration-test phase. -->
               <include>**/*IntegrationTests.java</include>
             </includes>
          </configuration>
       </execution>
    </executions>
  </plugin>

Good luck!

HDave