How to include test classes and configuration in my war to test integration using maven?

I currently have a maven web project in which I am trying to write integration tests. For the project structure, I defined test stubs in src / test / java , while spring bean definitions for these stubs sit in src / test / resources .

What I would like to do is that when I create my artifact of war, I would like all the classes of the test stub to be compiled and included in the war along with the spring bean definition files. I tried to do this with the maven war plugin, but the only thing I could copy was the resources. Simply put, I would like to use the path of the test class and include all these classes in my war file.

It seems that using the useTestClassPath option with the maven plugin for actuation will solve my problem, but the current project I'm working on is currently using Tomcat 6.0. Is there another maven plugin or a way that I can configure the maven war plugin to achieve my goal?

+7
source share
5 answers

You can also do it directly. This will add both test classes and test resources to WEB-INF / classes:

<plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <phase>process-test-classes</phase> <configuration> <target> <copy todir="${basedir}/target/classes"> <fileset dir="${basedir}/target/test-classes" includes="**/*" /> </copy> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> 

I also recommend that you place it in a separate profile, such as "integration", and also override the package name in this profile so that it cannot confuse a normal war without validation tests and testing.

Full example with profile here . You can run mvn clean package to have war war-it-test.war without tests included, or you can run mvn clean package -Pintegration for war war-it-test-integration.war for war with included tests.

+4
source

I believe the following configuration for the maven war plugin will do what you want. You copy your test classes to the WEB-INF / classes folder. You can even filter these resources.

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <executions> <execution> <id>generate-test-war</id> <phase>pre-integration-test</phase> <goals> <goal>war</goal> </goals> </execution> </executions> <configuration> <warSourceDirectory>${basedir}/src/test/webapp</warSourceDirectory> <warName>${project.artifactId}-test</warName> <webappDirectory>${basedir}/target/${project.artifactId}-test</webappDirectory> <primaryArtifact>false</primaryArtifact> <webResources> <resource> <directory>${basedir}/target/test-classes</directory> <targetPath>WEB-INF/classes</targetPath> </resource> </webResources> </configuration> </plugin> 

See http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

+4
source

You can use the maven build helper plugin to add additional folders to the path of the "normal" class.

But I would recommend creating a new folder for your integration test (for example, src / it / java) and adding this folder, but not the "normal" test folder (src / test / java) - the same for the resource folder.

+2
source

Instead of using the maven antrun plugin, you can use the maven resource plugin and configure it as follows:

 <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <phase>process-test-classes</phase> <id>test-classes</id> <goals> <goal>copy-resources</goal> </goals> <configuration> <overwrite>false</overwrite> <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> <resources> <resource> <directory>${project.build.directory}/test-classes</directory> </resource> </resources> </configuration> </execution> </executions> </plugin> 
0
source

Use the Tomcat 7 plugin with advanced class directory configurations.

  <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <additionalClasspathDirs> <additionalClasspathDir>${build.testOutputDirectory}</additionalClasspathDir> </additionalClasspathDirs> </configuration> <executions> <execution> <id>start-tomcat</id> <goals> <goal>run</goal> </goals> <configuration> <fork>true</fork> </configuration> </execution> </executions> </plugin> 
0
source

All Articles