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

て烟熏妆下的殇ゞ 提交于 2019-12-01 00:29:04

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


Instead of using the maven antrun plugin, you could instead use the maven resources plugin and configure it like this:

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



回答5:


Use Tomcat 7 plugin with additional classpath directories configuration.

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



回答6:


You can do this Configuration in pom.xml file you don't get any errors in pom.xml and adding test classes to our jar or war file.

    <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.eclipse.m2e</groupId>
                        <artifactId>lifecycle-mapping</artifactId>
                        <version>1.0.0</version>
                        <configuration>
                            <lifecycleMappingMetadata>
                                <pluginExecutions>
                                    <pluginExecution>
                                        <pluginExecutionFilter>
                                            <groupId>org.apache.maven.plugins</groupId>
                                            <artifactId>maven-antrun-plugin</artifactId>
                                            <versionRange>[1.7,)</versionRange>
                                            <goals>
                                                <goal>run</goal>
                                            </goals>
                                        </pluginExecutionFilter>
                                        <action>
                                            <execute />
                                        </action>
                                    </pluginExecution>
                                </pluginExecutions>
                            </lifecycleMappingMetadata>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>


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



回答7:


We need to add the below plugin to the pom.xml in order to add the test cases to jar. Thanks to @IvonSopov

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <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>

But after adding this line we got the build succeeded and also able to add the test case classes into the jar.. but the problem is in pom.xml it is showing as error like

Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-antrun-plugin:1.8:run (execution:default, phase: process-test-classes)

In order to remove this error we need to include the below plugin as a separate tag within the build tag. (not inside the plugins which we added earlier.)

    <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-antrun-plugin</artifactId>
                                        <versionRange>[1.7,)</versionRange>
                                        <goals>
                                            <goal>run</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

Now we can create the jar which includes the test classes without any errors.



来源:https://stackoverflow.com/questions/14052694/how-do-i-include-test-classes-and-configuration-in-my-war-for-integration-testin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!