Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration testing with maven: run jar before tests and terminate after

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).

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: I build my project both on Windows and Linux, so portability matters for me.

like image 691
Eugene Loy Avatar asked Apr 17 '14 11:04

Eugene Loy


1 Answers

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.

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>
like image 63
l8Again Avatar answered Sep 29 '22 09:09

l8Again