2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

Maven2 Integration

Cactus provides a way to execute your tests with Maven2. To execute the tests you can follow one of the approaches:

  • either write a build.xml, and use the Ant integration. After that you call the build.xml through the maven-antrun-plugin.
  • The second approach would be to directly call the cargo-maven2-plugin in order to start and stop the container.

Using the first approach

This section describes how to execute your test-cases from Maven calling the build.xml with the maven-antrun-plugin.

In your pom.xml add the following section:

<plugin>
   <artifactId>maven-antrun-plugin</artifactId>          			
   <executions>
     <execution>
       <configuration>
         <tasks>
           <ant antfile="build.xml"/>
         </tasks>
       </configuration>
     </execution>
   </executions>
</plugin>

For more information on how to use the maven-antrun-plugin see the official webpage of the maven-antrun-plugin.

After that see the cactus-ant-integration section to configure your build.xml file to cactify your war/ear and to execute your tests. Please note that this approach is more like a hack, which makes it not so flexible. If you need to pass some properties to the build.xml you can do it from the antrun configuration. Also in order to use the maven-antrun plugin to execute your tests, you need to have Ant installed and configured.

Second approach

This section desctibes the second approach to execute Cactus tests with Maven2.

From version 1.8.1 the Cactus project provides a maven2 plugin to help you manage your cactus tests. There are several mojos you can use: cactifywar, cactiwyear will help you to cactify your archives. After the cactification process you can use the cargo-maven2-plugin to start/stop the container and the maven-surefire-plugin to execute the tests.

Here is a sample configuration of the build section of you pom.xmlthat would:

  • Cactify your war.
  • Start a container.
  • Deploy the cactified archive.
  • Execute the cactus-tests.
  • Stop the container.
<build>
    <plugins>
      <plugin>
        <groupId>org.apache.cactus</groupId>
        <artifactId>cactus.integration.maven2</artifactId>
        <version>1.8.1-SNAPSHOT</version>
        <configuration>
          <srcFile>ready.war</srcFile>
          <destFile>cactifiedByMaven2.war</destFile>
        </configuration>
        <executions>
          <execution>
            <id>cactus-cactifywar</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>cactifywar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
        <executions>
          <execution>
            <id>start-container</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>start</goal>
            </goals>
          </execution>
          <execution>
            <id>stop-container</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <wait>false</wait>
          <timeout>20000</timeout>
          <container>
            <containerId>tomcat5x</containerId>
            <zipUrlInstaller>
              <url>http://apache.speedbone.de/tomcat/tomcat-5/v5.5.25/bin/apache-tomcat-5.5.25.zip</url>
              <installDir>${basedir}/install</installDir>
            </zipUrlInstaller>
          </container>
          <configuration>
             <deployables>
              <deployable>
                 <location>cactifiedByMaven2.war</location>
                 <pingURL>http://localhost:8080/test/</pingURL>
                 <properties>
                    <context>/test</context>
                 </properties>
              </deployable>
            </deployables>
          </configuration>
        </configuration> 
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <skip>true</skip>
        </configuration>
        <executions>
          <execution>
            <id>surefire-it</id>
            <phase>integration-test</phase>
            <goals>
              <goal>test</goal>
            </goals>
            <configuration>
              <skip>false</skip>
              <systemProperties>
                <property>
                  <name>cactus.contextURL</name>
                  <value>http://localhost:8080/test/</value>
                </property>
              </systemProperties>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>
      
What we do above is first define the cactus.integration.maven2 plugin in order to cactify the given war file. More on the cactifywar mojo you can have a look at the configuration page for this mojo. After that we attach Cargo's cargo-maven2-plugin to the <pre-integration-test> and the <post-integration-test>. We configure the plugin to start, deploy the artifact and stop the container. The last thing is to "unbind" the maven-surefire-plugin from the test phase and bind it with the integration-test phase. There are a few things to notice here:
  • First of all notice that since there is no post-package we are bound to attach the cactifywar goal to the pre-integration-test phase. In order to make it execute before the start-container execution we define it just before the start-container exection.
  • Cargo needs a deployable to deploy in the container. This deployable is resolved from the local maven repository (the one in [USER_HOME]/.m2/repository/). The cactifywar mojo will install the cactified archive in the local maven repository only if the maven installInLocalRepo parameter is set to true.
  • For the maven-surefire-plugin to execute cactus tests, there need to be a few system properties set.The cactus.contextURL property needs to be set! The value you have to set it, corresponds to the install url that you install your archives.