在集成测试不起作用之前启动apache服务器 [英] Starting apache server before integration testing not working

查看:92
本文介绍了在集成测试不起作用之前启动apache服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为GroupWebApplication的Web应用程序.使用pom.xml文件,我只是在创建应用程序之战,然后将其部署到tomcat服务器.这基本上是一个Maven项目.我已经为tomcat7 apache服务器添加了插件.

I have a web application named as GroupWebApplication. Using pom.xml file I'm just creating the war of my application and deploying to tomcat server. This is basically a maven project. I have added plugins for tomcat7 apache server.

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

Now what I want that I want to start the tomcat server then run the integration tests and after the completion of the integration tests I want to stop the server. Basically I'm using the testng.xml file of TestNg framework.testng.xml files contains all the classes for integration tests. Manually it's working fine, i.e. starting the server manually and running the test.

这是pom文件:

 <dependencies>

         <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.3.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>sGroupWebApplication Integration Test</finalName>
     <resources>
            <resource>
               <directory>src/main/resources</directory>
            </resource>
        </resources>
  <pluginManagement> 
    <plugins>
            <!-- Maven Tomcat Plugin -->
                        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>resource-dependencies</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeGroupIds>com.company.integration.test</includeGroupIds>
                            <includeArtifactIds>engine</includeArtifactIds>
                            <includes>**\/*.xpi,**\/*.exe,**\/*.so, **\/*.ftl,**\/*.css,**\/*.woff, **\/*.properties,**\/*.png,**\/chromedriver,**\/*.MF</includes>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <configuration>
      <url>http://localhost:8080/manager/text</url>
      <server>localhost</server>
      <path>/GroupWebApplication</path>
      <username>admin</username>
      <password>s3cret</password>
    </configuration>
    <executions>
          <execution>
            <id>tomcat7-run</id>
            <goals>
              <goal>run-war-only</goal>
            </goals>
            <phase>pre-integration-test</phase>
                <configuration>

                    <fork>true</fork> 
                </configuration>

           </execution>
           <execution>
            <id>tomcat7-shutdown</id>
            <goals>
              <goal>shutdown</goal>
            </goals>
            <phase>post-integration-test</phase>
          </execution>
        </executions>
</plugin> 
    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.4.2</version>
              </plugin>
            <!-- Java compiler version -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${target.jdk}</source>
                    <target>${target.jdk}</target>
                </configuration>
            </plugin>           
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                          <Project-Name>Integration App</Project-Name>
                          <Build-Version>${project.version}</Build-Version>
                          <Build-Date>${maven.build.timestamp}</Build-Date>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <outputEncoding>UTF-8</outputEncoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <configuration>
                    <reportOutputDirectory>javadoc</reportOutputDirectory>
                    <destDir>javadoc</destDir>
                </configuration>                
            </plugin>

        </plugins>
</pluginManagement> 
  </build>

testng.xml:

testng.xml:

  <suite name="INTEGRATION TEST Suite">
    <parameter name="logging"  value="SEVERE"/>
    <test name="GrpApiTest">
    <classes>
      <class name="com.company.integration.test.DummyIT"></class> 
    </classes>
    </test>
  </suite>

我得到的错误:

[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

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
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

基本上,这是此问题的重复项,但是它对我不起作用.

Basically this is duplicate of this question but it is not working for me.

推荐答案

向插件添加Tomcat 7和故障保护

我拿了您的POM并对其进行了修改,以使其正常运行(在我的机器上,我希望它也能在您的计算机上运行).这是我更改的内容:

Adding Tomcat 7 and Failsafe to plugins

I took your POM and modified it to make it work (on my machine, I hope it works on yours too). Here is what I changed:

  • build/pluginManagement/plugins元素中添加了 maven-failsafe-plugin .您必须按照使用页面中的指定,包括执行,否则执行将无法运行.

  • Added maven-failsafe-plugin in the build/pluginManagement/plugins element. You have to include the executions otherwise it does not run, as specified on the usage page.

<pluginManagement>
  ...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.17</version>
    <executions>
      <execution>
        <goals>
          <goal>integration-test</goal>
          <goal>verify</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  ...
</pluginManagement>

  • build/plugins元素中添加了 tomcat7-maven-plugin maven-failsafe-plugin ,否则它们将不会运行.通常,您应该在build/plugins元素中提及要用于构建的所有插件,从pluginManagement元素继承的插件可能无法运行.

  • Added tomcat7-maven-plugin and maven-failsafe-plugin in the build/plugins element, otherwise they won't run either. In general, you should mention all plugins you want to use for the build in the build/plugins element, plugins inherited from the pluginManagement element might not run.

    <build>
    ....
      <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
        </plugin>
      </plugins>
      ...
    </build>
    

  • tomcat7-maven-plugin 的升级版本为2.2,但它也可以与2.0一起使用.

  • Upgraded version of tomcat7-maven-plugin to 2.2 but it also works with 2.0.

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

    Here is the complete POM I used, for your copy-paste pleasure. ;)

    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                                 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.company</groupId>
      <artifactId>GroupWebApplication</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <properties>
        <target.jdk>1.7</target.jdk>
      </properties>
    
      <prerequisites>
        <maven>2.2.1</maven>
      </prerequisites>
    
      <dependencies>
        <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>6.3.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <finalName>sGroupWebApplication Integration Test</finalName>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
          </resource>
        </resources>
        <pluginManagement> 
          <plugins>
            <!-- Maven Tomcat Plugin -->
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-dependency-plugin</artifactId>
              <version>2.4</version>
              <executions>
                <execution>
                  <id>resource-dependencies</id>
                  <phase>initialize</phase>
                  <goals>
                    <goal>unpack-dependencies</goal>
                  </goals>
                  <configuration>
                    <includeGroupIds>com.company.integration.test</includeGroupIds>
                    <includeArtifactIds>engine</includeArtifactIds>
                    <includes>**\/*.xpi,**\/*.exe,**\/*.so, **\/*.ftl,**\/*.css,**\/*.woff, **\/*.properties,**\/*.png,**\/chromedriver,**\/*.MF</includes>
                    <outputDirectory>${project.build.directory}/classes</outputDirectory>
                  </configuration>
                </execution>
              </executions>
            </plugin>
            <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat7-maven-plugin</artifactId>
              <version>2.2</version>
              <configuration>
                <url>http://localhost:8080/manager/text</url>
                <server>localhost</server>
                <path>/GroupWebApplication</path>
                <username>admin</username>
                <password>s3cret</password>
              </configuration>
              <executions>
                <execution>
                  <id>tomcat7-run</id>
                  <goals>
                    <goal>run-war-only</goal>
                  </goals>
                  <phase>pre-integration-test</phase>
                  <configuration>
                    <fork>true</fork> 
                  </configuration>
                </execution>
                <execution>
                  <id>tomcat7-shutdown</id>
                  <goals>
                    <goal>shutdown</goal>
                  </goals>
                  <phase>post-integration-test</phase>
                </execution>
              </executions>
            </plugin> 
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-release-plugin</artifactId>
              <version>2.4.2</version>
            </plugin>
            <!-- Java compiler version -->
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <configuration>
                <source>${target.jdk}</source>
                <target>${target.jdk}</target>
              </configuration>
            </plugin>           
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-jar-plugin</artifactId>
              <version>2.4</version>
              <configuration>
                <archive>
                  <manifestEntries>
                    <Project-Name>Integration App</Project-Name>
                    <Build-Version>${project.version}</Build-Version>
                    <Build-Date>${maven.build.timestamp}</Build-Date>
                  </manifestEntries>
                </archive>
              </configuration>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-site-plugin</artifactId>
              <version>3.0</version>
              <configuration>
                <outputEncoding>UTF-8</outputEncoding>
              </configuration>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-javadoc-plugin</artifactId>
              <version>2.9.1</version>
              <configuration>
                <reportOutputDirectory>javadoc</reportOutputDirectory>
                <destDir>javadoc</destDir>
              </configuration>                
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-failsafe-plugin</artifactId>
              <version>2.17</version>
              <executions>
                <execution>
                  <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
          </plugins>
        </pluginManagement> 
        <plugins>
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </project>
    

    mvn验证输出

    这是在我的计算机上运行mvn verify的样子(为简便起见,省略了一些行).我创建了两个测试类,因此我们可以看到Surefire和Failsafe运行测试:DummyTest用于单元测试,DummyIT用于集成测试.

    mvn verify output

    This is how running mvn verify on my machine looks like (some lines omitted for brevity). I created two test classes so we can see Surefire and Failsafe run tests: DummyTest for unit tests and DummyIT for integration tests.

    [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
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    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
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    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, i.e. 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运行单元测试DummyTest
    2. Tomcat启动
    3. 故障安全运行集成测试DummyIT
    4. Tomcat正在停止
    1. Surefire running unit test DummyTest
    2. Tomcat starting
    3. Failsafe running integration test DummyIT
    4. Tomcat stopping

    我希望能对您有所帮助! :)

    I hope that helps! :)

    这篇关于在集成测试不起作用之前启动apache服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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