user2325154 user2325154 - 3 months ago 10
Java Question

Unit tests getting executed during integration test phase even after excluding them

I have a a unit test (

ProductDaoTest.java
) and an integration test (
ProductDaoIT.java
) in my maven application.

I would like to execute only the integration test during the
mvn verify
command call but the unit test also gets executed even after excluding it using the
<exclude>
tag in the
maven-failsafe-plugin
configuration.

How can I fix this problem?

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

Answer

In Maven, test step is before verify step in the lifecycle.
So it you don't skip this step, it is bound to execute.

If you want to skip test , either use -Dmaven.test.skip=tr‌​ue as khmarbaise suggested, either create a dedicated Maven profile for IT where you will ignore unit-tests in this way :

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>

Generally, you create a Maven profile for integration tests, so if it is the case, gathering all the configuration in a place is better that scattering it.