在集成测试之前使用Maven运行Jetty

时间:2013-12-24 14:09:05

标签: java maven junit jetty integration-testing

我将为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>

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

最诚挚的问候,

3 个答案:

答案 0 :(得分:2)

集成测试与mvn verify一起运行,而不是mvn test

答案 1 :(得分:1)

以下适用于我, 主要区别是:插件的较新版本(重要:不同的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>

答案 2 :(得分:0)

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

另外请记住使用空闲端口启动服务器而不是硬编码某些值,否则测试会失败,因为端口可能在另一个版本上使用。如果你搜索,有很多方法。