Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include test classes and configuration in my war for integration testing using maven?

I currently have a maven web project that I am attempting to write integration tests for. For the structure of the project, I've defined test stubs under src/test/java, whilst the spring bean definitions for these stubs sit under src/test/resources.

What I would like to do, is that when I build my war artifact I'd like all of the test stub classes to be compiled and included in the war along with the spring bean definition files. I've tried to do it with the maven war plugin but the only things I've been able to copy are the resources. Simply put, I'd like to make use of the test class path and include all these classes in my war file.

It seems the useTestClassPath option with the maven jetty plugin would 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 I can configure the maven war plugin to achieve my objective?

like image 904
Peter Nguyen Avatar asked Dec 27 '12 09:12

Peter Nguyen


Video Answer


3 Answers

You can also do it straightforwardly. This will add both test classes and test resources to the 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 you place it into separate profile like "integration" and also to override the package name in that profile to not be able to confuse normal war without tests packaged in and the testing war.

The full example with profile is here. You may run mvn clean package to have a war war-it-test.war without tests included, or you may run mvn clean package -Pintegration to have a war war-it-test-integration.war for the war with tests included.

like image 162
Ivan Sopov Avatar answered Sep 27 '22 19:09

Ivan Sopov


I believe the following configuration for the maven war plugin would do what you want. You copy your test-classes to your WEB-INF/classes folder. You can even filter those 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

like image 35
vlfig Avatar answered Sep 27 '22 21:09

vlfig


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

But I would recommend to create an new folder for your integration test (for example src/it/java), and add this folder, but not the "normal" test folder (src/test/java) -- the same for the resources folder.

like image 28
Ralph Avatar answered Sep 27 '22 20:09

Ralph