vendredi 23 juin 2017

Maven plus Spring Boot plus Multi Module failsafe Integrated Testing

I have a Spring Boot multi-module project that meets all of the requirements except Integration testing. The unit tests, defined in src/test work fine in surefire as part of the test phase.

Multiple hours of experimenting with failsafe have not brought about the desired results. failsafe is not able to find any of the tests. I am assuming this is based on:

  1. The test classes not being part of the .jar file associated with each of the modules.
  2. No way I can figure out to get the test .jar file on the class path where failsafe will look for tests.

My project:

project folder
--pom.xml
|
----application
------pom.xml
------src
--------main
--------test
|
----model
------pom.xml
|
------data
--------pom.xml
--------src
----------main
----------test
|
------repository
--------pom.xml
--------src
----------main
----------test

My parent pom:

<project>

   ....

   <artifactId>master</artifactId>

   ....

   <modules>
      <module>model</module>
      <module>application</module>
   </modules>

   ....

   <dependencyManagement>
      <dependencies>
         <!-- Model -->
         <dependency>
            <groupId>xxx.xxx.xxx</groupId>
            <artifactId>data</artifactId>
            <version>${project.version}</version>
         </dependency>
         <dependency>
            <groupId>xxx.xxx.xxx</groupId>
            <artifactId>repository</artifactId>
            <version>${project.version}</version>
         </dependency>
         <!-- Application -->
         <dependency>
            <groupId>xxx.xxx.xxx</groupId>
            <artifactId>application</artifactId>
            <version>${project.version}</version>
         </dependency>
   </dependencyManagement>

   ....

   <build>
      <!-- defined here and then used on a module-by-module basis -->
      <pluginManagement>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-jar-plugin</artifactId>
               <executions>
                  <execution>
                     <goals>
                        <goal>test-jar</goal>
                     </goals>
                  </execution>
               </executions>
            </plugin>

            <!-- Configure failsafe -->
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-failsafe-plugin</artifactId>
               <configuration>
                  <includes>
                     <include>**/*Tests.java</include>
                     <include>**/*Test.java</include>
                  </includes>
                  <excludes>
                     <exclude>**/Abstract*.java</exclude>
                  </excludes>
                  <additionalClasspathElements>
                     <additionalClasspathElement>
                        ${project.basedir}/../../model/data/target/data-${project.version}-tests.jar
                     </additionalClasspathElement>
                  </additionalClasspathElements>
                  <dependenciesToScan>
                     <dependency>
                        com.optum.cirrus:repository
                     </dependency>
                  </dependenciesToScan>
               </configuration>
               <executions>
                  <execution>
                     <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                     </goals>
                  </execution>
               </executions>
            </plugin>
            </plugin>
         </plugins>
      </pluginManagement>

      ....

      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <executions>
               <execution>
                  <goals>
                     <goal>test-jar</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
</project>

Note that I create a test-jar for all of the modules along with a jar based on the main code.

The module pom files are:

<project>

   <parent>
      <artifactId>master</artifactId>
   </parent>

   ....

   <artifactId>model</artifactId>
   <packaging>pom</packaging>

   <modules>
      <module>data</module>
      <module>repository</module>
   </modules>

   ....

</project>

<project>

   ....

   <parent>
      <artifactId>model</artifactId>
   </parent>

   <artifactId>data</artifactId>
   <packaging>jar</packaging>
   <name>data</name>

   ....

</project>

<project>

   ....

   <parent>
      <artifactId>model</artifactId>
   </parent>

   <artifactId>repository</artifactId>
   <packaging>jar</packaging>
   <name>repository</name>

   <dependencies>
      <dependency>
         <groupId>xxx.xxx.xxxx</groupId>
         <artifactId>data</artifactId>
      </dependency>
   </dependencies>

   ....

</project>

<project>

   ....

  <parent>
      <artifactId>model</artifactId>
   </parent>

   <artifactId>master</artifactId>
   <packaging>jar</packaging>
   <name>application</name>

   <dependencies>
      <dependency>
         <groupId>xxx.xxx.xxxx</groupId>
         <artifactId>resources</artifactId>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
         </plugin>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
               <execution>
                  <goals>
                     <goal>repackage</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
</project> 

failsafe only runs as part of the application build and after the spring boot repackaging has been run, though both the original and the repackaged jar files are available.

When I run maven with debug on I see the repackaged application jar plus the resources jar [I am assuming this is there based on the dependency element defined on failsafe] plus the resources-test.jar [assumed to be there based on the .

I am at a loss on how to disfigure failsafe such that it can find the test classes that are included in the .jar file defined in the element.

Or if there was a way to create a second artifact from, for example, the resources pom.xml that would create the test.jar I could pass that to failsafe as the dependency. Note that 'create a separate module that just had src/test in it that was dependent on the module that had src/main in it' might work but that seems like a real kludge to change what should be a standard model for source code to fit a maven model.

Live can not be that bizarre so obviously I am doing something wrong. Any suggestions would be appreciated.

Thanks.

Aucun commentaire:

Enregistrer un commentaire