如何在使用Maven进行集成测试的战争中包括测试类和配置? [英] How do I include test classes and configuration in my war for integration testing using maven?

查看:79
本文介绍了如何在使用Maven进行集成测试的战争中包括测试类和配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个正在尝试为其编写集成测试的Maven Web项目.对于项目的结构,我已经在 src/test/java 下定义了测试存根,而这些存根的s​​pring bean定义位于 src/test/resources 下.

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.

我想做的是,当我构建战争构件时,我希望所有测试存根类都可以编译并包含在战争中以及Spring bean定义文件.我已经尝试使用Maven War插件来做到这一点,但是我唯一能复制的就是资源. 简而言之,我想利用测试类路径并将所有这些类包括在我的war文件中.

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.

似乎带有maven jetty插件的useTestClassPath选项可以解决我的问题,但是我正在处理的当前项目当前正在使用Tomcat 6.0.有没有其他的Maven插件,或者我可以配置Maven War插件来实现自己的目标?

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?

推荐答案

您也可以直接进行操作.这会将测试类和测试资源都添加到WEB-INF/classs中:

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.

完整的配置文件示例为此处.您可以运行mvn clean package进行不包含测试的战争war-it-test.war,也可以运行mvn clean package -Pintegration对包含测试的战争进行战争war-it-test-integration.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.

这篇关于如何在使用Maven进行集成测试的战争中包括测试类和配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆