与Maven的集成测试

时间:2018-06-21 15:04:52

标签: java maven integration-testing pom.xml maven-failsafe-plugin

我希望能够分别运行单元测试和集成测试。

问题是,即使从我的孩子pom.xml中,我在运行时仍指定了该内容:

mvn verify -P integration-test

我只想在src/it/java中运行测试,但是它只执行所有测试。

我的目录是:

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

pom.xml (子级)

<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> 

我从主目录运行mvn verify -P integration-test,我只想测试名为“ it”的文件夹中的脚本。

1 个答案:

答案 0 :(得分:2)

这就是我们对我的项目所做的。

首先,/ src / it / java是非标准的,因此我们将所有测试源放在/ src / test / java下。

在共享测试源根目录下,可以通过对测试类采用命名约定来使集成测试和单元测试保持独立。例如,对于单元测试,使用* UT.java或UT _ *。java,对于集成测试使用* IT.java或IT _ *。java。

然后,您可以同时使用故障保护和surefire插件上的include / excludes选项,以在每个插件中执行正确的测试。在surefire插件中,仅包括UT源,而排除IT源。在故障保护中,只需执行相反的操作即可。

例如:

  <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>