Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在maven中集成测试期间启动外部进程_Java_Maven 2_Build Process_Automated Tests_Integration Testing - Fatal编程技术网

Java 在maven中集成测试期间启动外部进程

Java 在maven中集成测试期间启动外部进程,java,maven-2,build-process,automated-tests,integration-testing,Java,Maven 2,Build Process,Automated Tests,Integration Testing,我想要一个Maven项目的完全自动化集成测试。集成测试要求在运行之前启动外部(依赖于平台的)程序。理想情况下,外部程序将在单元测试完成后终止,但这不是必需的 是否有一个Maven插件来实现这一点?其他想法?您可以使用该插件。在内部,您将使用ant的任务 像这样的 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin&l

我想要一个Maven项目的完全自动化集成测试。集成测试要求在运行之前启动外部(依赖于平台的)程序。理想情况下,外部程序将在单元测试完成后终止,但这不是必需的

是否有一个Maven插件来实现这一点?其他想法?

您可以使用该插件。在内部,您将使用ant的任务

像这样的

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.2</version>
    <executions>
      <execution>
        <phase> <!-- a lifecycle phase --> </phase>
        <configuration>

          <tasks>
            <apply os="unix" executable="cmd">
              <arg value="/c"/>
              <arg value="ant.bat"/>
              <arg value="-p"/>
            </apply>
            <apply os="windows" executable="cmd.exe">
              <arg value="/c"/>
              <arg value="ant.bat"/>
              <arg value="-p"/>
            </apply>
          </tasks>

        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

org.apache.maven.plugins

.

我目前正在开发一个更具体的插件,它很容易被“降级”为一个简单的外部任务执行器,但是。。。还有很多其他的事情要考虑。

  • 你怎么知道这个过程实际上已经开始了
  • 您如何处理返回码
  • 如何确保executor插件首先运行(将其绑定到测试编译阶段)
  • 我确信如果我真的开始开发这个插件,将会有更多的插件,但是真的需要一个通用的执行器吗

    更新:


    我想有。。。CodeHaus有一套优秀的Maven插件。这是您想要的:。

    是否要启动应用程序服务器?如果您正在进行servlet开发,并且希望部署由此产生的WAR进行集成测试,那么可以查看cargo maven插件及其。

    当我自己做这件事时,我经常建立一个多模块项目(尽管严格来说这不是必需的),并将所有集成测试封装到一个模块中。然后,我使用概要文件(或不使用概要文件)启用模块,这样它就不会阻止立即生成的“是的,我知道我破坏了它”版本

    以下是功能测试模块中的pom-根据您的意愿:

    <?xml version="1.0"?><project>
      <parent>
        <artifactId>maven-example</artifactId>
        <groupId>com.jheck</groupId>
        <version>1.5.0.4-SNAPSHOT</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.jheck.example</groupId>
      <artifactId>functional-test</artifactId>
      <name>Example Functional Test</name>
      <packaging>pom</packaging>
    
      <dependencies>
        <dependency>
          <groupId>com.jheck.example</groupId>
          <artifactId>example-war</artifactId>
          <type>war</type>
          <scope>provided</scope>
          <version>LATEST</version>
        </dependency>
        <dependency>
          <groupId>httpunit</groupId>
          <artifactId>httpunit</artifactId>
          <version>1.6.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>testCompile</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <executions>
              <execution>
                <phase>integration-test</phase>
                <goals>
                  <goal>test</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
    
          <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>0.3</version>
            <configuration>
              <wait>false</wait> <!-- don't pause on launching tomcat... -->
              <container>
                <containerId>tomcat5x</containerId>
                <log>${project.build.directory}/cargo.log</log>
                <zipUrlInstaller>
                  <!--
                  <url>http://www.apache.org/dist/tomcat/tomcat-5/v5.0.30/bin/jakarta-tomcat-5.0.30.zip</url>
                   -->
                   <!-- better be using Java 1.5... -->
                  <url>http://www.apache.org/dist/tomcat/tomcat-5/v5.5.26/bin/apache-tomcat-5.5.26.zip</url>
    
                  <installDir>${installDir}</installDir>
                </zipUrlInstaller>
              </container>
              <configuration>
                <!-- where the running instance will be deployed for testing -->
                <home>${project.build.directory}/tomcat5x/container</home>
              </configuration>
            </configuration>
    
            <executions>
              <execution>
                <id>start-container</id>
                <phase>pre-integration-test</phase>
                <goals>
                  <goal>start</goal>
                  <goal>deploy</goal>
                </goals>
                <configuration>
                  <deployer>
                    <deployables>
                      <deployable>
                        <groupId>com.jheck.example</groupId>
                        <artifactId>example-war</artifactId>
                        <type>war</type>
                        <!-- <properties>
                          <plan>${basedir}/src/deployment/geronima.plan.xml</plan>
                        </properties> -->
                        <pingURL>http://localhost:8080/example-war</pingURL>
                      </deployable>
                    </deployables>
                  </deployer>
                </configuration>
              </execution>
              <execution>
                <id>stop-container</id>
                <phase>post-integration-test</phase>
                <goals>
                  <goal>stop</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
    </project>
    
    
    maven示例
    com.jheck
    1.5.0.4-快照
    4.0.0
    com.jheck.example
    功能测试
    示例功能测试
    聚甲醛
    com.jheck.example
    榜样战
    战争
    假如
    最新的
    高温超导单元
    高温超导单元
    1.6.1
    测试
    org.apache.maven.plugins
    maven编译器插件
    测试编译
    org.apache.maven.plugins
    maven surefire插件
    集成测试
    测试
    org.codehaus.cargo
    cargo-maven2-plugin
    0.3
    假的
    tomcat5x
    ${project.build.directory}/cargo.log
    http://www.apache.org/dist/tomcat/tomcat-5/v5.5.26/bin/apache-tomcat-5.5.26.zip
    ${installDir}
    ${project.build.directory}/tomcat5x/container
    启动容器
    预集成测试
    开始
    部署
    com.jheck.example
    榜样战
    战争
    http://localhost:8080/example-战争
    停止容器
    整合后测试
    停止
    
    您可能希望将实际的集成测试绑定到maven生命周期的集成测试阶段。如果您使用故障保护插件(如恰当命名的故障保护插件)进行实际测试,那么您可以运行以下阶段:

    预集成测试:启动外部应用程序(使用exec插件或此处的其他建议之一)

    集成测试:使用故障保护插件运行实际的集成测试

    集成后测试:关闭外部应用程序并执行任何其他必要的清理

    验证:让故障保护插件验证测试结果,并在此时使构建失败


    使用exec插件非常简单,诀窍是在后台启动应用程序。在下一阶段开始测试之前,您应该小心确保应用程序已完全启动。不幸的是,在后台运行时,启动应用程序并确保它运行得足够好并不总是一项简单的任务,具体操作方法取决于应用程序。它通常涉及应用程序中的自定义代码。

    一个有趣的、非直观的antrun插件使用。我希望有更干净的东西,但这可能对我有用。Maven倾向于为特定任务提供插件。您试图运行的命令可能有插件。命令是特定于项目的可执行文件,因此需要一个通用的“执行”插件。