Java 集成测试前使用Maven运行Jetty
我将为RESTAPI创建集成测试。所以,我想在测试之前运行Jetty,并在测试之后停止它。每次测试我都有连接拒绝错误。我的POM.XML的构建部分写在下面:Java 集成测试前使用Maven运行Jetty,java,maven,junit,jetty,integration-testing,Java,Maven,Junit,Jetty,Integration Testing,我将为RESTAPI创建集成测试。所以,我想在测试之前运行Jetty,并在测试之后停止它。每次测试我都有连接拒绝错误。我的POM.XML的构建部分写在下面: <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <configuration> <scanI
<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>
org.mortbay.jetty
maven jetty插件
0
福
9999
8010
60000
/性能分析器服务
起动码头
预集成测试
跑
0
真的
停靠码头
整合后测试
停止
org.apache.maven.plugins
maven surefire插件
2.16
性能分析器服务it
集成测试
测试
假的
你能帮我解决这个问题吗
致以最诚挚的问候,也许不是最好的方式,但我在过去通过在您的测试的
@BeforeClass
中编程启动Jetty来做到这一点。请看这里:
另外,请记住使用空闲端口来启动服务器,而不是硬编码某些值,否则测试会在CI上失败,因为端口可能正在另一个版本上使用。如果您进行搜索,有很多方法可以实现它。集成测试使用
mvn verify
运行,而不是mvn test
以下内容对我有用,
主要区别在于:插件的更新版本(重要信息:不同的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>
org.eclipse.jetty
jetty maven插件
9.2.5.v20141112
11079
停止
11080
/
起动码头
预集成测试
开始
0
真的
停靠码头
整合后测试
停止
您的测试是否指向正确的端口?我还记得看到一个插件,它可以让你保留一个开放端口,并设置一个变量,你可以传递给你的容器和测试…是的,我再次检查,测试指向8010,相同的端口是的,我尝试了你所说的方法,它可以工作;然而,我正在寻找Maven的解决方案。。。无论如何,感谢您的解决方案。对我来说,唯一的小问题是守护进程元素应该作为/plugin/configuration的一部分提供,而不是/plugin/executions/execution/configuration XML路径。是的,问题中的配置使用Surefire插件(mvn test
);对于集成测试,应使用故障保护插件(mvn-verify
)。正确的配置显示在中。