Hector Esteban
Hector Esteban

Reputation: 1111

Integration test with maven

I want to be able to run Unit tests and integration tests separately.

The problem is that even though from my child pom.xml I specify that, when I run:

mvn verify -P integration-test

I want to just run the tests in src/it/java, but it just executes all the tests.

My directories are:

edms-pro
  pom.xml (the general one)
  ...
  rest-servides
    src
      it
        java
        resources
    pom.xml (the child one)

pom.xml (the child one)

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.everis</groupId>
    <artifactId>edms-pro</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

  <name>Big Content Services</name>

  <artifactId>rest-services</artifactId>
  <packaging>jar</packaging>

  <properties>
    <skip.unit.tests>false</skip.unit.tests>
    <skip.integration.tests>true</skip.integration.tests>
  </properties>

  <profiles>
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
    </profile>
    <profile>
      <id>integration-test</id>
      <properties>
        <!--
            Only integration tests are run when the integration-test profile is active
        -->
        <skip.integration.tests>false</skip.integration.tests>
        <skip.unit.tests>true</skip.unit.tests>
      </properties>
    </profile>
  </profiles>

  <dependencies>
    <!-- Spring components -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>log4j-over-slf4j</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>io.prometheus</groupId>
      <artifactId>simpleclient_spring_boot</artifactId>
      <version>${prometheus.version}</version>
    </dependency>
    <dependency>
      <groupId>io.prometheus</groupId>
      <artifactId>simpleclient_hotspot</artifactId>
      <version>${prometheus.version}</version>
    </dependency>
    <dependency>
      <groupId>com.ryantenney.metrics</groupId>
      <artifactId>metrics-spring</artifactId>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>${swagger2.version}</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>${swagger2.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- Lombok Java extensions -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <scope>provided</scope>
    </dependency>
    <!-- Apache Commons -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
    </dependency>
    <!-- JSR-305 annotations for Software Defect Detection -->
    <dependency>
      <groupId>com.google.code.findbugs</groupId>
      <artifactId>jsr305</artifactId>
    </dependency>
    <!-- Google Guava core libraries for Java -->
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
    </dependency>
    <!-- Logging -->
    <dependency>
      <groupId>net.logstash.logback</groupId>
      <artifactId>logstash-logback-encoder</artifactId>
    </dependency>
    <!-- Apache http components -->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
    </dependency>
    <!-- Testing -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-library</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.everis</groupId>
      <artifactId>edms-mocks</artifactId>
      <version>${com.everis.edms.version}</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>com.everis</groupId>
          <artifactId>edms-backend-couchbase</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20160810</version>
    </dependency>
    <dependency>
      <groupId>com.auth0</groupId>
      <artifactId>java-jwt</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>com.microsoft.azure</groupId>
      <artifactId>adal4j</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.everis</groupId>
      <artifactId>multitenancy-test</artifactId>
      <version>${com.everis.edms.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <classifier>${dist.classifier}</classifier>
        </configuration>
      </plugin>
      <!-- A partir d'aqui afegir per Hector respecte diseny original-->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <!--<version>1.12</version> -->
        <executions>
          <execution>
            <id>add-integration-test-sources</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <!-- Configures the source directory of our integration tests -->
              <sources>
                <source>src/it/java</source>
              </sources>
            </configuration>
          </execution>
          <!-- Add a new resource directory to our build -->
          <execution>
            <id>add-integration-test-resources</id>
            <phase>generate-test-resources</phase>
            <goals>
              <goal>add-test-resource</goal>
            </goals>
            <configuration>
              <!-- Configures the resource directory of our integration tests -->
              <resources>
                <resource>
                  <filtering>true</filtering>
                  <directory>src/it/resources</directory>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <!--<version>3.5</version> -->
        <configuration>
          <!--<source>${jdk.version}</source>
          <target>${jdk.version}</target> -->
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <!--<version>2.19.1</version> -->
        <configuration>
          <!--<groups>com.testwithspring.intermediate.unittests.UnitTest</groups>-->
          <!--
              Skips unit tests if the value of skip.unit.tests
              property is true
          -->
          <skipTests>${skip.unit.tests}</skipTests>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>${maven-failsafe-plugin.version}</version>
        <executions>
          <execution>
            <!--<id>integration-tests</id>-->
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
            <configuration>
              <testSourceDirectory>src/it/java</testSourceDirectory>
              <!--
              <includes>
                <include>src/it/*.java</include>
              </includes> -->
              <!--<groups>com.testwithspring.intermediate.integrationtests.IntegrationTest</groups>-->
              <!--
                  Skips integration tests if the value of skip.integration.tests
                  property is true
              -->
              <skipTests>${skip.integration.tests}</skipTests>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project> 

I run mvn verify -P integration-test from the main directory and I would like just to test the scripts inside the folders called "it".

Upvotes: 1

Views: 4810

Answers (1)

Michael Peacock
Michael Peacock

Reputation: 2104

Here's what we do on my projects.

First, /src/it/java is non-standard, so we put all test source under /src/test/java.

With a shared test source root directory, you can keep integration tests and unit test separate by adopting a naming convention for your test classes. For example, *UT.java, or UT_*.java for unit tests, and *IT.java or IT_*.java for integration tests.

Then, you can use both the includes/excludes options on the failsafe and surefire plug-ins to execute the correct tests in each. In the surefire plugin, include only the UT sources, and exclude the IT sources. In failsafe, simply do the opposite.

For example:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <!--<version>2.19.1</version> -->
    <configuration>
          <includes>
            <include>**/*UT.java</include>
          </includes> 
          <excludes>
            <exclude>**/*IT.java</exclude>
          </excludes> 
      <skipTests>${skip.unit.tests}</skipTests>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>${maven-failsafe-plugin.version}</version>
    <executions>
      <execution>
        <!--<id>integration-tests</id>-->
        <goals>
          <goal>integration-test</goal>
          <goal>verify</goal>
        </goals>
        <configuration>
          <includes>
            <include>**/*IT.java</include>
          </includes> 
          <excludes>
            <exclude>**/*UT.java</exclude>
          </excludes> 
          <skipTests>${skip.integration.tests}</skipTests>
        </configuration>
      </execution>
    </executions>
  </plugin>

Upvotes: 4

Related Questions