MAVEN EXPERIENCE - Maven and integration test

Table Of Contents


Integration and Functional Testing using profile

In this article we show an example bout how to separate classic unit tests from integration unit test using only maven surefire plugin and profiles

Separate Unit tests from Integration Tests

Example below assumes that the Unit Test classes have as postfix Test while Integration Test classes have as postfix Integration Test but you can use whatever naming convention that you prefer.
It include only the Unit Test classes excluding the Integration test ones.

configure surefire plugin
<plugin>
 <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>
    <skip>false</skip>
    <includes>
     <include>**/*Test.java</include>
    </includes>
    <excludes>
     <exclude>**/*IntegrationTest.java</exclude>
   </excludes>
  </configuration>
 </execution>
</executions>
</plugin>

Create profile that contains Integration Tests configuration

Example below create a integration-test profile that, when activated, will include the Integration Test classes.

integration-test profile definition
<profile>
 <id>itest</id>
 <build>
  <plugins>
   <plugin>
   <!-- Separates the unit tests from the integration tests. -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <executions>
     <execution>
      <id>integration-tests</id>
      <phase>integration-test</phase>
      <goals>
       <goal>test</goal>
      </goals>
    <configuration>
     <skip>false</skip>
     <includes>
      <include>**/*IntegrationTest.java</include>
     </includes>
    </configuration>
   </execution>
  </executions>
 </plugin>
</plugins>
</build>
</profile>

Let's put all together

pom.xml
<project>
<build>
  <plugins>
    <plugin>
     <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>
        <skip>false</skip>
        <includes>
         <include>**/*Test.java</include>
        </includes>
        <excludes>
         <exclude>**/*IntegrationTest.java</exclude>
       </excludes>
      </configuration>
     </execution>
    </executions>
    </plugin>
  </plugins>
</build>

<profiles>
  <profile>
   <id>itest</id>
   <build>
    <plugins>
     <plugin>
     <!-- Separates the unit tests from the integration tests. -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <executions>
       <execution>
        <id>integration-tests</id>
        <phase>integration-test</phase>
        <goals>
         <goal>test</goal>
        </goals>
      <configuration>
       <skip>false</skip>
       <includes>
        <include>**/*IntegrationTest.java</include>
       </includes>
      </configuration>
     </execution>
    </executions>
   </plugin>
  </plugins>
  </build>
  </profile>
</profiles>
</project>

run integration test

mvn -Pitest integration-test

Labels

maven maven Delete
dependencies dependencies Delete
housekeeping housekeeping Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.