使用maven进行集成测试:在测试之前运行jar并在之后终止 [英] Integration testing with maven: run jar before tests and terminate after

查看:116
本文介绍了使用maven进行集成测试:在测试之前运行jar并在之后终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可运行的jar,我希望在集成测试开始之前在一个新进程中运行(在 pre-integration-test 上)并在集成后终止它测试完成(在后整合测试)。

I have a runnable jar that I want to run in a new process before my integration test start (on pre-integration-test) and get it terminated after my integration tests finish (on post-integration-test).

我可以使用的一件事是 maven-antrun-plugin exec-maven-plugin 预集成测试上启动新流程但是我如何终止呢?

One of the things I could use is maven-antrun-plugin or exec-maven-plugin to start new process on pre-integration-test but how do I terminate it?

也许有更好的解决方案可以实现我的目标?

Maybe there is a better solution for what I am trying to achieve?

PS:我在Windows和Linux上都构建了我的项目,因此可移植性对我很重要。

PS: I build my project both on Windows and Linux, so portability matters for me.

推荐答案

你可以使用 maven-process-plugin 由BV开源,用于启动和停止集成前后阶段的任何流程tively。自述文件中有一个例子可以完全符合您的要求(启动一个可运行的jar)。希望有所帮助。

You can use maven-process-plugin open sourced by BV to start and stop any process in pre and post-integration phase respectively. There is an example in the readme that does exactly what you want (start a runnable jar). Hope that helps.

主要想法是写一个有两个目标的maven插件 - 开始和停止。开始在预集成阶段运行并运行您想要运行的任何进程。 Java的流程构建器可用于启动运行jar等流程。将已启动的流程存储在数据结构(如Map或Stack)中。 停止将停止它已启动的所有进程。由于这是一个普遍的问题,我建议使用上面的插件来轻松启动和停止任何进程以进行集成测试。只需添加以下插件:

The main idea is to write a maven plugin with two goals - start, and stop. Start runs in your pre-integration-phase and runs any process you would like to run. Java's process builder can be used to start processes like running jars, etc. Store the started Process in a datastructure such as a Map or a Stack. 'Stop' will stop all the processes it has started. Since this is such a generic problem, I would recommend the above plugin for starting and stopping any processes easily for your integration tests. Simply add the following plugin:

<plugin>
        <groupId>com.bazaarvoice.maven.plugins</groupId>
        <artifactId>process-exec-maven-plugin</artifactId>
        <version>0.4</version>
        <executions>
            <!--Start process-->
            <execution>
                <id>start-jar</id>
                <phase>pre-integration-test</phase>
                <goals><goal>start</goal></goals>
                <configuration>
                    <workingDir>app</workingDir>
                    <arguments>
                        <argument>java</argument>
                        <argument>-jar</argument>
                        <argument>app.jar</argument>
                    </arguments>
                </configuration>
            </execution>
            <!--Stop Process-->
            <execution>
                <id>stop-jar-process</id>
                <phase>post-integration-test</phase>
                <goals><goal>stop-all</goal></goals>
            </execution>
        </executions>
</plugin>

这篇关于使用maven进行集成测试:在测试之前运行jar并在之后终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆