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

Lit*_*ird 5 java testng maven-2 maven-3 maven

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

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

这是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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

[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:
Run Code Online (Sandbox Code Playgroud)

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

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

Kol*_*l00 6

将Tomcat 7和Failsafe添加到插件中

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

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

    <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>
    
    Run Code Online (Sandbox Code Playgroud)
  • 在元素中添加了tomcat7-maven-pluginmaven-failsafe-pluginbuild/plugins,否则它们也不会运行.通常,您应该提到要用于build/plugins元素构建的所有插件,从pluginManagement元素继承的插件可能无法运行.

    <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>
    
    Run Code Online (Sandbox Code Playgroud)
  • tomcat7-maven-plugin的升级版本为2.2,但它也适用于2.0.

完整的POM

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

<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>
Run Code Online (Sandbox Code Playgroud)

mvn验证输出

这就是mvn verify我的机器上运行的样子(为简洁起见省略了一些行).我创建了两个测试类,因此我们可以看到Surefire和Failsafe运行测试:DummyTest用于单元测试和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

-------------------------------------------------------
 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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

你可以看到:

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

我希望有所帮助!:)