Maven-在JUnit测试之前将Web应用程序部署到tomcat [英] Maven - deploy webapp to tomcat before JUnit test
问题描述
我有提供网络服务的webapp.我想使用SoapUI执行JUnit测试,以检查此服务是否正常运行.
但是要测试Web服务应用程序,必须将其部署到我的Tomcat 7服务器上.
I have webapp which provides web service. I want to perform JUnit tests with SoapUI to check if this service is working properly.
But to test web service application has to be deployed to my Tomcat 7 server.
我不知道如何配置Maven进行战争,然后将其部署到tomcat(理想情况是:为此运行单独的tomcat实例),然后运行JUnit测试.
我将不胜感激.
I have no idea how to configure maven to build war, then deploy it to tomcat (ideally: to run separate tomcat instance for this) and then to run JUnit tests.
I will appreciate any help.
我正在使用Maven 2.2.1
I am using maven 2.2.1
推荐答案
关于如何使用Maven处理这种类型的集成测试,有很多想法.
There are a number of schools of thought as to how to handle this type of integration test with Maven.
我应该指出,当您将应用程序部署到应用程序服务器时,您不再处于单元测试的领域.由于整个应用程序都部署在一个容器中,因此您正在测试这两个组件的集成.
I should point out that when you are deploying an application to an application server, you are not in the realm of unit testing any more. Because the entire application is being deployed within a container, you are testing the integration of those two components.
现在使用JUnit运行集成测试没什么错(尽管您可能会遇到一些限制,例如 unit tests 不必在乎顺序单个测试的组合-假设您正确编写了它们-因此JUnit通过不保证任何执行顺序来强制执行 ...在Java 1.7之前,执行顺序是由测试方法的顺序意外地暗示的.类,但它不是JUnit合同的一部分...有些人在找到单元测试重点时,转而使用其他测试框架进行 integration 测试,例如TestNG.的JUnit妨碍了他们的测试开发)
Now there is nothing wrong with using JUnit for running integration tests (though there are some limitations that you may hit, for example unit tests should not care about the sequencing of individual tests - assuming you are writing them correctly - so JUnit enforces this by not guaranteeing any sequence of execution... prior to Java 1.7 the execution order was accidentally implied by the order of test methods within a class, but it was not part of the JUnit contract... Some people switch to other testing frameworks for their integration tests, e.g. TestNG, if they find the unit test focus of JUnit is getting in the way of their test development)
要记住的关键点是Maven生命周期使用test
阶段执行 unit 测试.
The key point to keep in mind is that the Maven lifecycle uses the test
phase for the execution of unit tests.
关于集成测试,关于使用Maven处理测试的正确方法,有两种(一半)思想流派.
When it comes to integration tests there are two (and a half) schools of thought as to the right way to handle the tests with Maven.
该思想流派使用package
之后的阶段来启动容器,运行集成测试,拆除容器,最后检查测试结果并在测试失败的情况下使构建失败.
This school of thought uses the phases after package
to start up a container, run the integration tests, tear down the container, and finally check the test results and fail the build in the event of test failures.
永远不要运行mvn integration-test
,因为这不会正确拆下容器,在您每次想输入mvn integration-test
的时候,您实际上想输入mvn verify
的时候(哦,看,它更短,更容易还输入...奖金)
NEVER EVER RUN mvn integration-test
as that will not tear down the container correctly, any time you think you want to type mvn integration-test
you actually want to type mvn verify
(oh look, it's shorter and easier to type also... bonus)
因此,您可以执行以下操作:
So with this you do the following:
- 绑定 tomcat7:run 使用
=true
- 绑定故障保护:集成测试进入
integration-test
阶段 - 绑定 tomcat7:shutdown 进入
post-integration-test
阶段 - 绑定故障保护:验证到
verify
阶段.
- Bind tomcat7:run to the
pre-integration-test
phase withfork
=true
- Bind failsafe:integration-test to the
integration-test
phase - Bind tomcat7:shutdown to the
post-integration-test
phase - Bind failsafe:verify to the
verify
phase.
要获得额外的布朗尼积分,您可以使用 build -helper-maven-plugin:reserve-network-port 绑定到validate
阶段,以确保测试服务器在未使用的网络端口上启动,然后对测试资源使用资源过滤以通过该端口进行测试或使用通过传递的系统属性systemPropertyVariables ,以使端口号可用于测试.
For extra brownie points you would use build-helper-maven-plugin:reserve-network-port bound to the validate
phase to ensure that the test server is started on an unused network port and then either use resource filtering against the test resources to pass the port through to the tests or use a system property passed through systemPropertyVariables to make the port number available to the tests.
- 清理Maven版本
- 如果测试失败,则无法发布项目
- 如果集成测试太慢而无法运行每个版本,则可以将集成测试移到单独的配置文件中(按照惯例称为
run-its
).
- 很难从IDE运行测试.所有集成测试都在
IT
中开始/结束,而Maven知道使用Surefire在Test
中开始/结束运行测试以及使用Failsafe在IT
中开始/结束运行测试,但您的IDE可能不会.此外,您的IDE不会为您启动容器,因此您必须手动进行大量工作才能真正实际运行测试. -
调试测试可能需要附加两个调试器,例如一个用于调试在容器中运行的应用程序,另一个用于调试测试用例.
- Hard to run the tests from an IDE. All the integration tests start/end in
IT
and while Maven knows to run tests starting/ending inTest
with Surefire and run tests starting/ending inIT
with Failsafe, your IDE probably doesn't. Additionally, your IDE is not going to start the container for you, so you have to do a lot of work by hand to actually run the tests manually. Debugging the tests potentially requires attaching two debuggers, e.g. one to debug the application running in container and the other to debug the test cases.
mvnDebug -Dmaven.failsafe.debug=true verify
将您的测试耦合到Maven构建过程.
Couples your tests to the Maven build process.
这种思路将集成测试移到依赖于war
模块的单独模块中,并将war
复制到测试资源中,例如使用 dependency:copy-dependencies
绑定到generate-test-resources
阶段加上要测试的Tomcat7依赖项.
This school of thought moves the integration tests into a separate module that depends on the war
module and copies the war
into the test resources using, e.g. dependency:copy-dependencies
bound to the generate-test-resources
phase coupled with a Tomcat7 dependency to test against.
测试用例本身使用嵌入式模式
- 测试可以在IDE中运行
- 集成测试与单元测试是分开的,因此要求IDE运行所有测试不会启动较慢的测试
- 仅当您经过
package
阶段时,才会重建war
工件,因此,使用IDE时,您至少需要定期运行mvn clean package
来刷新测试中的代码. - 集成测试的失败不会破坏
war
模块的构建,因此您最终可能会释放损坏的war
工件,然后使集成测试模块的反应堆构建失败.有些人通过在src/it
中安装集成测试模块并使用Maven Invoker插件运行测试来解决此问题,尽管这样做提供的IDE集成效果较差,所以我不建议这样做. - 很难从Maven获得综合的测试覆盖率报告.
- 必须对容器进行编码,以便在测试用例中自行启动/停止.
- The
war
artifact is only rebuilt if you go past thepackage
phase, consequently, you need to run at leastmvn clean package
periodically to refresh the code under test when using the IDE. - The failure of the integration tests does not break the build of the
war
module, so you can end up releasing a brokenwar
artifact and then have the reactor build fail for the integration test module. Some people counteract this issue by having the integration test module withinsrc/it
and using Maven Invoker Plugin to run the tests... though that provides a poorer IDE integration, so I do not recommend that line. - Hard to get a consolidated test coverage report from Maven.
- Have to code the container start/stop yourself from within your test cases.
这是两种方法的一种混合.
This is a kind of hybrid of the two approaches.
您使用Failsafe执行测试,但是测试本身负责启动和停止要在其中测试的Tomcat7容器.
You use Failsafe to execute the tests, but the tests themselves are responsible for starting and stopping the Tomcat7 container that you want to test in.
- 不必在Maven pom中配置服务器启动/停止
- IDE可以安全地运行所有测试(尽管集成测试可能会比较慢,并且您可能不希望运行它们,但这并不意味着除非出现测试失败,否则它们都会全部失败)
- 更容易从IDE调试测试(只需附加一个进程,IDE通常通过提供特殊的测试运行程序使调试调试变得容易)
- 必须对容器进行编码,以便在测试用例中自行启动/停止
我希望以上内容可以帮助您了解所拥有的选项.可能还有其他一些调整,但通常,以上内容被认为是目前与Maven进行集成测试的最佳实践.
I hope the above helps you understand the options you have. There may be other tweaks but in general the above are considered the best practice(s) for integration testing with Maven at present.
这篇关于Maven-在JUnit测试之前将Web应用程序部署到tomcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!