42

UT = Unit Tests IT = Integration Tests. All my Integration test classes are annotated with @Category(IntegrationTest.class)

My goal is:

mvn clean install => runs UT and not IT

mvn clean install -DskipTests=true => no tests are executed

mvn clean deploy => runs UT and not IT

mvn clean test => runs UT and not IT

mvn clean verify => runs UT and IT

mvn clean integration-test => runs IT and UT are not executed

mvn clean install deploy => runs UT and not IT

pom properties:

<junit.version>4.12</junit.version>
<surefire-plugin.version>2.18.1</surefire-plugin.version>
<failsafe-plugin.version>2.18.1</failsafe-plugin.version>
  1. Compiler:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <compilerArgument>-Xlint:all</compilerArgument>
            <showWarnings>true</showWarnings>
            <showDeprecation>true</showDeprecation>
        </configuration>
    </plugin>
    
  2. Unit Tests:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${surefire-plugin.version}</version>
        <configuration>
            <excludedGroups>com.xpto.IntegrationTest</excludedGroups>
        </configuration>
    </plugin>
    
  3. Integration Tests:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>${failsafe-plugin.version}</version>
        <configuration>
            <groups>com.xpto.IntegrationTest</groups>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                </goals>
                <configuration>
                    <includes>
                        <include>**/*.class</include>
                    </includes>
                </configuration>
            </execution>                
        </executions>              
    </plugin>        
    

My results are:

mvn clean install => OK

mvn clean install -DskipTests=true => OK

mvn clean deploy => runs UT and not IT

mvn clean test => OK

mvn clean verify => NOK ... only UT are executed but IT also needs to be executed

mvn clean integration-test => NOK ... UT are executed and should not and IT aren't executed but should be executed

mvn clean install deploy => OK

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Leonel
  • 2,796
  • 5
  • 25
  • 36
  • Is it possible for you to add some sample tests? Perhaps a UT and IT. I'm wondering if how they are named/organized/annotated is part of the issue. – Nick DeFazio Oct 23 '15 at 17:51
  • 2
    At least part of your requirements will probably not be doable ... you say "mvn clean integration-test" should run IT and *not* UT, but the maven lifecycle is a chain ... when you run `mvn integration-test` it runs through generate-sources, compile, compile-test, test, package, etc all the way up to and including integration-test. Your UT should be super-quick anyway, so why would you want to skip them? – dcsohl Oct 23 '15 at 18:38
  • First i would suggest to use the correct naming convention to name a unit test `*Test.java` and integration tests `*IT.java` (see docs of maven-surefire-plugin/maven-failsafe-plugin). Furthermore if you do a thing like this : `mvn clean install deploy` this does not make sense and didn't understand the maven build life cycle. If you do `mvn deploy` the install is part of the life cycle which in result means just use `mvn deploy`. If a call `mvn clean deploy` will run your UT but not IT's your configuration is wrong. – khmarbaise Oct 23 '15 at 19:27
  • I'm using Naming conventions for maven-surefire-plugin (unit tests). Naming conventions for maven-failsafe-plugin (integration tests) as suggested and It's working perfectly with khmarbaise solution. But now my Integration Tests are not intercepted by the cobertura plugin. Any suggestion? Maybe using JaCoCo instead? – Leonel Oct 26 '15 at 15:28

1 Answers1

35

The solution is this:

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.19.1</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
          <version>2.19.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
   <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <skip>${surefire.skip}</skip>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

This will let you control which is executed.

running UT and IT's:

mvn clean verify

running IT's but NOT UT's

mvn clean verify -Dsurefire.skip=true 

running UT but not IT's:

mvn clean verify -DskipITs=true 

You need to follow the naming conventions of the plugins which makes life easier.

Naming conventions for maven-surefire-plugin (unit tests). Naming conventions for maven-failsafe-plugin (integration tests).

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • It's working perfectly now. But now my Integration Tests are not intercepted by the cobertura plugin. Any suggestion? Maybe using JaCoCo instead? – Leonel Oct 26 '15 at 14:11
  • For [cobertura-maven-plugin](http://www.mojohaus.org/cobertura-maven-plugin/) i would suggest to look into the documentation of you can use JaCoCo ?... – khmarbaise Oct 26 '15 at 15:51
  • Related question http://stackoverflow.com/questions/33349864/merging-integration-and-unit-test-reports-with-jacoco – Leonel Oct 26 '15 at 16:38
  • Is this right? maven-surefire-plugin has only a test goal. It has neither integration-test nor a verify goal. When I run the example it throws an error: `Could not find goal 'integration-test' in plugin org.apache.maven.plugins:maven-surefire-plugin:2.19.1 among available goals.` I am using **Apache Maven 3.6.0** – Igorski Apr 20 '19 at 10:03
  • The integration tests should be run with Maven Failsafe Plugin instead... https://maven.apache.org/surefire/maven-failsafe-plugin/index.html – khmarbaise Apr 20 '19 at 12:25