在集成测试不起作用之前启动apache服务器

我有一个名为GroupWebApplication的Web应用程序。 使用pom.xml文件我只是创建我的应用程序的战争并部署到tomcat服务器。 这基本上是一个maven项目。 我已经为tomcat7 apache服务器添加了插件。

现在我想要启动tomcat服务器,然后运行集成测试,在集成测试完成后,我想停止服务器。 基本上我正在使用TestNg的testng.xml文件framework.testng.xml文件包含集成测试的所有类。 手动它工作正常,即手动启动服务器并运行测试。

这是pom文件:

  org.testng testng 6.3.1 test    sGroupWebApplication Integration Test   src/main/resources       org.apache.maven.plugins maven-dependency-plugin 2.4   resource-dependencies initialize  unpack-dependencies   com.company.integration.test engine **\/*.xpi,**\/*.exe,**\/*.so, **\/*.ftl,**\/*.css,**\/*.woff, **\/*.properties,**\/*.png,**\/chromedriver,**\/*.MF ${project.build.directory}/classes      org.apache.tomcat.maven tomcat7-maven-plugin 2.0  http://localhost:8080/manager/text localhost /GroupWebApplication admin s3cret    tomcat7-run  run-war-only  pre-integration-test  true    tomcat7-shutdown  shutdown  post-integration-test     org.apache.maven.plugins maven-release-plugin 2.4.2    org.apache.maven.plugins maven-compiler-plugin 3.1  ${target.jdk} ${target.jdk}    org.apache.maven.plugins maven-jar-plugin 2.4    Integration App ${project.version} ${maven.build.timestamp}      org.apache.maven.plugins maven-site-plugin 3.0  UTF-8    org.apache.maven.plugins maven-javadoc-plugin 2.9.1  javadoc javadoc      

testng.xml文件:

          

我得到的错误:

 [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ GroupWebApplication --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory C:\Users\t_ddbc\Documents\clone_4_ju ne\demo\GroupWebApplication\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ GroupWebApplication --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 6 source files to C:\Users\t_ddbc\Documents\clone_4_june\demo\ GroupWebApplication\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ GroupWebApplication --- [INFO] Surefire report directory: C:\Users\t_ddbc\Documents\clone_4_june\demo\GroupWebApplication\target\surefire- reports ------------------------------------------------------- TESTS ------------------------------------------------------- Running TestSuite Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNGMapConfigur ator@203e25d3 org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.In vocationTargetException; nested exception is java.lang.reflect.InvocationTargetE xception: null java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces sorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray( ReflectionUtils.java:189) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke (ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(Provi derFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(Fork edBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java: 75) Caused by: java.lang.IllegalStateException: 

在终端命令我正在我的项目的根目录mvn verify

基本上这是这个问题的重复,但它不适合我。

将Tomcat 7和Failsafe添加到插件中

我拿了你的POM并对其进行了修改以使其正常工作(在我的机器上,我希望它也可以在你的机器上运行)。 这是我改变的:

  • build/pluginManagement/plugins元素中添加了maven-failsafe-plugin 。 您必须包含执行,否则它不会运行,如使用页面上指定的那样。

      ...  org.apache.maven.plugins maven-failsafe-plugin 2.17    integration-test verify     ...  
  • build/plugins元素中添加了tomcat7-maven-pluginmaven-failsafe-plugin ,否则它们也不会运行。 通常,您应该在build/plugins元素中提及要用于构建的所有插件,从pluginManagement元素inheritance的插件可能无法运行。

      ....   org.apache.tomcat.maven tomcat7-maven-plugin   org.apache.maven.plugins maven-failsafe-plugin   ...  
  • tomcat7-maven-plugin的升级版本为2.2,但它也适用于2.0。

完整的POM

这是我使用的完整POM,为您的复制粘贴乐趣。 ;)

  4.0.0 com.company GroupWebApplication 0.0.1-SNAPSHOT war  1.7   2.2.1    org.testng testng 6.3.1 test    sGroupWebApplication Integration Test   src/main/resources       org.apache.maven.plugins maven-dependency-plugin 2.4   resource-dependencies initialize  unpack-dependencies   com.company.integration.test engine **\/*.xpi,**\/*.exe,**\/*.so, **\/*.ftl,**\/*.css,**\/*.woff, **\/*.properties,**\/*.png,**\/chromedriver,**\/*.MF ${project.build.directory}/classes      org.apache.tomcat.maven tomcat7-maven-plugin 2.2  http://localhost:8080/manager/text localhost /GroupWebApplication admin s3cret    tomcat7-run  run-war-only  pre-integration-test  true    tomcat7-shutdown  shutdown  post-integration-test     org.apache.maven.plugins maven-release-plugin 2.4.2    org.apache.maven.plugins maven-compiler-plugin 3.1  ${target.jdk} ${target.jdk}    org.apache.maven.plugins maven-jar-plugin 2.4    Integration App ${project.version} ${maven.build.timestamp}      org.apache.maven.plugins maven-site-plugin 3.0  UTF-8    org.apache.maven.plugins maven-javadoc-plugin 2.9.1  javadoc javadoc    org.apache.maven.plugins maven-failsafe-plugin 2.17    integration-test verify         org.apache.tomcat.maven tomcat7-maven-plugin   org.apache.maven.plugins maven-failsafe-plugin     

mvnvalidation输出

这就是我的机器上运行mvn verify样子(为简洁起见省略了一些行)。 我创建了两个测试类,因此我们可以看到Surefire和Failsafe运行测试: DummyTest用于unit testing, DummyIT用于集成测试。

 [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building GroupWebApplication 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ ... [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ GroupWebApplication --- [INFO] Surefire report directory: /home/sotest/target/surefire-reports ------------------------------------------------------- TESTS ------------------------------------------------------- Running DummyTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.185 sec Results: Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-war-plugin:2.1.1:war (default-war) @ GroupWebApplication --- [INFO] Packaging webapp [INFO] Assembling webapp [GroupWebApplication] in [/home/sotest/target/sGroupWebApplication Integration Test] [INFO] Processing war project [INFO] Copying webapp resources [/home/sotest/src/main/webapp] [INFO] Webapp assembled in [14 msecs] [INFO] Building war: /home/sotest/target/sGroupWebApplication Integration Test.war [INFO] WEB-INF/web.xml already added, skipping [INFO] [INFO] --- tomcat7-maven-plugin:2.2:run-war-only (tomcat7-run) @ GroupWebApplication --- [INFO] Running war on http://localhost:8080/GroupWebApplication [INFO] Creating Tomcat server configuration at /home/sotest/target/tomcat [INFO] create webapp with contextPath: /GroupWebApplication Aug 20, 2014 9:40:18 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8080"] Aug 20, 2014 9:40:18 PM org.apache.catalina.core.StandardService startInternal INFO: Starting service Tomcat Aug 20, 2014 9:40:18 PM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.47 Aug 20, 2014 9:40:19 PM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-bio-8080"] [INFO] [INFO] --- maven-failsafe-plugin:2.17:integration-test (default) @ GroupWebApplication --- [INFO] Failsafe report directory: /home/sotest/target/failsafe-reports ------------------------------------------------------- TESTS ------------------------------------------------------- Running DummyIT Configuring TestNG with: TestNGMapConfigurator Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.162 sec - in DummyIT Results: Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- tomcat7-maven-plugin:2.2:shutdown (tomcat7-shutdown) @ GroupWebApplication --- Aug 20, 2014 9:40:20 PM org.apache.coyote.AbstractProtocol pause INFO: Pausing ProtocolHandler ["http-bio-8080"] Aug 20, 2014 9:40:20 PM org.apache.catalina.core.StandardService stopInternal INFO: Stopping service Tomcat Aug 20, 2014 9:40:20 PM org.apache.coyote.AbstractProtocol stop INFO: Stopping ProtocolHandler ["http-bio-8080"] [INFO] [INFO] --- maven-failsafe-plugin:2.17:verify (default) @ GroupWebApplication --- [INFO] Failsafe report directory: /home/sotest/target/failsafe-reports [WARNING] File encoding has not been set, using platform encoding UTF-8, ie build is platform dependent! [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.015s [INFO] Finished at: Wed Aug 20 21:40:20 SGT 2014 [INFO] Final Memory: 22M/291M [INFO] ------------------------------------------------------------------------ 

你可以看到:

  1. Surefire运行unit testingDummyTest
  2. Tomcat开始
  3. 故障安全运行集成测试DummyIT
  4. Tomcat停了下来

我希望有所帮助! 🙂

我在这看到两个问题。 第一个也是一个较小的问题是,当您使用“mvn verify”时,您正在使用“mvn integration-test”。

如果你看一下Maven生命周期 ,你会发现生命周期阶段是“预集成 – 测试”,“集成测试”,“集成后测试”,然后是“validation”。 通过仅运行“集成测试”,“后集成测试”挂钩将不会运行,并且停止服务器的集成后测试步骤将不会执行。

您还应该使用Failsafe插件,而不是Surefire插件。 Surefire插件专为unit testing而设计,并在“测试”生命周期阶段执行,该阶段发生在预集成测试阶段之前。 换句话说,您的测试在Tomcat服务器启动之前运行。 Failsafe,基本上是Surefire,但对于集成测试,在“集成测试”阶段运行,因此它们将在您在“预集成 – 测试”阶段启动Tomcat服务器之后运行。 值得注意的是,在“validation”步骤之前,Failsafe不会使构建失败,以便可以进行“集成后测试”工作。 因此,在使用Failsafe插件时,使用“verify”而不是“integration-test”作为maven命令是非常重要的。

我也有一个导致此错误的问题,但对我来说,结果是缺少运行测试到测试文件夹的帐户的文件访问权限。

可能需要检查一下

根据您发布的内容,您已经定义了pluginManagement所有插件,这些插件在第一眼就可以正确定义版本和配置。 但是要执行tomcat插件,你应该在构建区域中至少提到一次。

    ...    org.apache.tomcat.maven tomcat7-maven-plugin     

除了使用maven-failsafe-plugin而不是maven-surefire-plugin进行集成测试和调用mvn verify等其他建议。此外,使用更多最新的插件版本也是明智之举。