DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Thomas has posted 33 posts at DZone. View Full User Profile

Set Up Maven To Run Unit Tests And Integration Tests Separately

01.19.2011
| 13838 views |
  • submit to reddit
        Test classes ending with 'IntegrationTests.java' will be run in the integration-test phase

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.test.maven</groupId>
  <version>0.1-SNAPSHOT</version>
  <name>maven-tests</name>
  <artifactId>maven-tests</artifactId>
  
  <properties>
    <maven.test.skip>false</maven.test.skip>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <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...) -->
          <skip>true</skip>
        </configuration>
        <executions>
          <execution>
            <id>unit-tests</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
            <configuration>
              <skip>${maven.test.skip}</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>
              <skip>${maven.test.skip}</skip>
              <includes>
                <!-- Include integration tests within integration-test phase -->
                <include>**/*IntegrationTests.java</include>
              </includes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Source: http://stackoverflow.com/questions/1399240/how-do-i-get-my-maven-integration-tests-to-run    

Comments

Mayuresh Hajirnis replied on Fri, 2012/07/13 - 6:54pm

Glad to find this. I spent almost 3-4 hours figuring out why my tests were running twice.