Starting apache server before integration testing not working

我们两清 提交于 2019-12-04 09:33:33

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:

  • 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>
    
  • 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>
    
  • Upgraded version of tomcat7-maven-plugin to 2.2 but it also works with 2.0.

Complete 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 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] ------------------------------------------------------------------------

You can see:

  1. Surefire running unit test DummyTest
  2. Tomcat starting
  3. Failsafe running integration test DummyIT
  4. Tomcat stopping

I hope that helps! :)

I see two problems here. The first and lesser problem is that you're using "mvn integration-test" when you should be using "mvn verify".

If you look at the Maven lifecycles, you will see that the lifecycle phases are ordered "pre-integration-test", "integration-test", "post-integration-test", then "verify". By running just "integration-test", the "post-integration-test" hook will not run, and the post-integration-test step to stop the server will not execute.

You should also be using the Failsafe plugin, not the Surefire plugin. The Surefire plugin is designed for unit tests, and executes during the "test" lifecycle phase, which occurs prior to the pre-integration-test phase. In other words, your tests are running prior to the Tomcat server starting. Failsafe, which is basically Surefire but for integration tests, runs during the "integration-test" phase, so they will run after you've started the Tomcat server in the "pre-integration-test" phase. It is also worth noting that Failsafe won't fail the build until the "verify" step so that your "post-integration-test" work can occur. So it's extra important to use "verify" instead of "integration-test" as your maven command when using the Failsafe plugin.

I also had an issue that resulted in this error, but for me it turned out to be missing file access rights for the account running the tests to the test folder.

Might be something to check out

Based on what you posted you have defined all plugins in the pluginManagement which is on the first glance correct to define versions and configuration. But to get tomcat plugin executed you should mentioned that at least once in your build area as well.

<project..>

  <build>
    <pluginManagement>
    ...
    </pluginManagement>

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

</project>

Apart from other suggestions like using maven-failsafe-plugin instead of maven-surefire-plugin for integration tests and calling mvn verify etc. Furthermore using more up-to-date plugin versions would also be wise.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!