使用maven进行集成测试:在测试之前运行jar并在之后终止

时间:2014-04-17 11:19:55

标签: java maven testing integration-testing

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

我可以使用maven-antrun-pluginexec-maven-plugin之一来启动pre-integration-test上的新流程,但如何终止呢?

对于我想要实现的目标,或许有更好的解决方案?

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

6 个答案:

答案 0 :(得分:9)

您可以使用BV开源的maven-process-plugin分别在集成前和集成后阶段启动和停止任何过程。自述文件中有一个例子可以完全满足您的需求(启动一个可运行的jar)。希望有所帮助。

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

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

答案 1 :(得分:1)

我使用maven-antrun-plugin解决了这个问题。

如果你的后台进程是java进程,你可以使用java ant目标并给它一个停止超时。如果你知道你的集成测试会在N秒后结束,那么fork&amp;产生进程并在超时后停止进程(N)

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>start-my-Application</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <java jar="target/myapp-${project.version}.jar"
                                  spawn="true"
                                  fork="true"
                                  timeout="120">
                                <sysproperty key="env" value="test" />
                                <arg value="--xyz" /> <arg value="123" />
                            </java>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>

答案 2 :(得分:1)

对我来说,它与exec-maven-plugin中的 exec 目标和 async 设置一起工作。

 <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
      <execution>
        <id>Start your java process</id>
        <goals>
          <goal>exec</goal>
        </goals>
        <phase>pre-integration-test</phase>
      </execution>
    </executions>
    <configuration>
      <async>true</async>
      <executable>java</executable>
      <arguments>
        <argument>-classpath</argument>
        <classpath/><!-- classpath from the module dependencies-->
        <argument>your.MainClass</argument>
      </arguments>
      <environmentVariables>
        <somevariable>isSet</someVariable>
      </environmentVariables>
    </configuration>
</plugin>

运行Maven结束后,该过程将自动终止。您可能还需要查看 asyncDestroyOnShutdown 选项

答案 3 :(得分:0)

如果这只是Linux,我会将PID写入文件,然后稍后使用此PID终止进程。

我的猜测是你可以在Windows上实现类似的东西,但你需要一些非标准的工具。

另一种方法是在新进程中打开一个套接字并在此套接字上侦听kill命令。这适用于所有平台,但您需要修改JAR。

注意:如果需要并行运行多个集成测试,这也会导致问题。

答案 4 :(得分:0)

最简单的方法是在外部jar中包含某种停止机制(与tomcat的停止端口相比)。

这样你就不需要处理PID或任何东西了。

最简单,最节省资源的方法是在预定义(可覆盖)端口上打开服务器套接字。

答案 5 :(得分:0)

通过调用bash杀死它

  subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 26
                buildToolsVersion '26.0.0'
            }
        }
    }
  }


  configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-v4:27.1.0'
    }
  }