我一直试图将我们的集成测试转换为使用Maven Exec插件来启动服务器,但是只有在执行了集成测试之后,它才会启动HTTP,即使我指定了pre-integration-test。
问题
Q1。还有其他方法可以让集成测试在之后启动吗?
Q2。是否有一种方法可以让集成阶段等待服务器启动?
Q3。Maven Exec和Bazaar Maven Exec插件有什么区别?
注释:以前使用过bazaar maven exec插件,但这是错误的,并且似乎无法使它在Jenkins上的JDK11.0.9中恢复工作。
下面是我们POM的一个片段,我们有各种其他插件创建jar并启动MockServer。
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>exec</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<asyncDestroyOnShutdown>true</asyncDestroyOnShutdown>
<executable>java</executable>
<commandlineArgs>-Dserver.port=8089 -jar /gitRepos/public-api/target/api-jar-with-dependencies.jar</commandlineArgs>
<async>true</async>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<executions>
<execution>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<excludedGroups>${it.excluded}</excludedGroups>
<argLine>--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED</argLine>
<argLine>
--illegal-access=permit
</argLine>
</configuration>
</plugin>
</plugins>
<!-- this what was previously working. -->
<!-- <plugin>-->
<!-- <groupId>com.bazaarvoice.maven.plugins</groupId>-->
<!-- <artifactId>process-exec-maven-plugin</artifactId>-->
<!-- <version>0.9</version>-->
<!-- <configuration>-->
<!-- <processLogFile>${project.build.directory}/my-log.log</processLogFile>-->
<!-- </configuration>-->
<!-- </plugin>-->对我来说奇怪的是HTTP在Integration之后启动,就像其他插件阻止服务器启动一样。见下面的日志。
[DEBUG] Freed 4 thread-local buffer(s) from thread: nioEventLoopGroup-3-20
[DEBUG] Freed 4 thread-local buffer(s) from thread: nioEventLoopGroup-3-19
[DEBUG] Freed 5 thread-local buffer(s) from thread: nioEventLoopGroup-3-28
[INFO] [main] 13:13:37.594 [main] INFO c.i.p.s.Server - Grizzly ThreadPool for listener grizzly set to 24 worker threads.
[INFO] [main] 13:13:37.724 [main] INFO o.g.g.http.server.NetworkListener - Started listener bound to [0.0.0.0:8089]
[INFO] [main] 13:13:37.726 [main] INFO o.g.grizzly.http.server.HttpServer - [HttpServer] Started.发布于 2021-02-12 18:25:44
谢谢你在下面的评论你的问题。
在深入研究了你的问题后,我决定重复你的怀疑,并得出结论,在exec-maven-plugin中,一切都如预期的那样工作。
让我解释一下--我没有修改您的pom.xml片段,只实现了一个小的Java类,它每100 ms打印一行。
public class Ocp {
public static void main(String[] args) {
IntStream.range(1, 100).forEach(i -> {
System.err.println("====== " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}结果如下:
~/dev/git/so-mvn$ mvn clean verify
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< de.mle:so-mvn >----------------------------
[INFO] Building stackoverflow 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
***intentionally left out***
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ so-mvn ---
[INFO] Building jar: /home/marco/dev/git/so-mvn/target/so-mvn-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- exec-maven-plugin:3.0.0:exec (exec) @ so-mvn ---
[INFO]
[INFO] --- maven-failsafe-plugin:3.0.0-M5:integration-test (default) @ so-mvn ---
====== 1
====== 2
====== 3
====== 4
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
====== 5
====== 6
====== 7
====== 8
[INFO] Running so.mvn.Ocp11IT
====== 9
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 s - in so.mvn.Ocp11IT
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-failsafe-plugin:3.0.0-M5:verify (default) @ so-mvn ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.398 s
[INFO] Finished at: 2021-02-12T19:17:24+01:00
[INFO] ------------------------------------------------------------------------正如您所看到的,流程在集成测试阶段(Q1 & Q2)之前就已经开始了。只有由于您的async配置,集成测试才不会等待我的存根服务器完成启动。所以Maven这里一切都很好。
您提到的bazaarvoice插件只是做了一个小技巧来减轻这一点。它有两个特性,等待它们为集成测试“开始”。它们是:
healthcheckUrl:推荐,但可选。您应该提供一个健康检查url,所以插件等待直到您的进程的健康检查都是绿色的。如果没有提供,插件在继续运行之前等待waitAfterLaunch秒。waitAfterLaunch:可选。这指定启动进程后等待的最长时间(秒)。如果指定了healthcheckUrl,那么一旦健康检查通过,它就会继续运行。默认为30秒。在您的示例中,摘要只需等待默认的30秒,这似乎就足以让您的外部服务器出现。exec-maven-plugin中没有这样的“等待”功能,但是您可以例如,在集成测试本身(Q3)中以编程方式移动这个尚未丢失的健康检查,就像在现实生活中对依赖(微)服务的健康检查一样。另一个选项(我喜欢的选项)是,移动对接容器中的所有外部服务器依赖项或进程,然后在(码头)插件级上再次使用健康检查。
如果您查看我的示例项目中的等待一个ES容器出现和相应的集成测试,这可能是一个很好的开始。
对于您来说,在插件级别没有任何等待时间的另一个选择是真正复杂的实用程序库,它在断言自身内附带了很好的等待特征或轮询间隔。
https://stackoverflow.com/questions/66102443
复制相似问题