【问题标题】:Running Jetty using Maven before Integration Test在集成测试之前使用 Maven 运行 Jetty
【发布时间】:2014-01-12 18:47:03
【问题描述】:

我将为 REST API 创建集成测试。所以,我想在测试之前运行 Jetty 并在测试之后停止它。我有每个测试的连接被拒绝错误。我的 POM.XML 的构建部分写在下面:

    <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <configuration>
    <scanIntervalSeconds>0</scanIntervalSeconds>
    <stopKey>foo</stopKey>
    <stopPort>9999</stopPort>
    <connectors>
        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
        <port>8010</port>
        <maxIdleTime>60000</maxIdleTime>
        </connector>
    </connectors>
    <contextPath>/performance-parser-service</contextPath>
    </configuration>
    <executions>
            <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <scanintervalseconds>0</scanintervalseconds>    
                        <daemon>true</daemon>
                    </configuration>
            </execution>
            <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                            <goal>stop</goal>
                    </goals>
            </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <executions>
        <execution>
            <id>performance-parser-service-it</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <skip>false</skip>
            </configuration>
        </execution>
    </executions>
</plugin>

你能帮我解决这个问题吗?

最好的问候,

【问题讨论】:

  • 您的测试是否指向正确的端口?我还记得看到一个插件可以让你保留一个开放端口并设置一个变量,你可以将它传递给你的容器和测试......
  • 是的,我再次检查并测试指向 8010,同一个端口

标签: java maven junit jetty integration-testing


【解决方案1】:

使用mvn verify 运行集成测试,而不是mvn test

【讨论】:

  • 是的,问题中的配置使用了Surefire插件(mvn test);对于集成测试,应使用 Failsafe 插件 (mvn verify)。正确的配置见Maven Failsafe Usage Guide
【解决方案2】:

以下对我有用, 主要区别是:较新版本的插件(重要:不同的groupId)并使用目标start而不是run

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.2.5.v20141112</version>
    <configuration>
        <stopPort>11079</stopPort>
        <stopKey>STOP</stopKey>
        <httpConnector>
            <port>11080</port>
        </httpConnector>
        <webApp>
            <contextPath>/</contextPath>
        </webApp>
    </configuration>
    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
            <configuration>
                <scanIntervalSeconds>0</scanIntervalSeconds>
                <daemon>true</daemon>
            </configuration>
        </execution>
        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

【讨论】:

  • 对我有用,唯一的小问题是守护程序元素应该作为 /plugin/configuration 的一部分给出,而不是 /plugin/executions/execution/configuration XML 路径。
【解决方案3】:

也许不是最好的方法,但我过去曾通过在您的测试的@BeforeClass 中以编程方式启动 Jetty 来做到这一点。见这里:http://www.eclipse.org/jetty/documentation/current/embedded-examples.html#embedded-one-webapp

还记得使用空闲端口来启动服务器,而不是硬编码某些值,否则测试在您的 CI 上失败,因为端口可能在另一个构建中使用。如果你搜索的话,有很多方法。

【讨论】:

  • 是的,我试过你说的方法,它有效;但是,我正在为此寻找 Maven 解决方案...无论如何,感谢您的解决方案。
猜你喜欢
  • 1970-01-01
  • 2013-03-20
  • 2015-06-01
  • 1970-01-01
  • 2014-06-21
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
相关资源
最近更新 更多