Howto define jetty port for integration tests


Today I found a simple hack to define the jetty-port for a web-application integration test via command line.

The problem was the defined „default“ jetty port (here 8080) … on the build system this port was already bind … so the integration test always failed😦.

My first idea was to call maven with external system property „jetty.port“ … (this worked already to start the web application on different ports):

mvn clean integration-test -Pintegration-test -Djetty.port=1234

The maven profile is here:

...
  <profile>
      <id>integration-test</id>
      <build>
        <defaultGoal>integration-test</defaultGoal>
        <plugins>
          <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <executions>
              <execution>
                <id>integration-tests</id>
                <phase>integration-test</phase>
                <goals>
                  <goal>test</goal>
                </goals>
                <configuration>
                  <forkMode>once</forkMode>
                  <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/Integration-Suite.xml</suiteXmlFile>
                  </suiteXmlFiles>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>${jetty.version}</version>
            <executions>
              <execution>
                <id>start-jetty</id>
                <phase>pre-integration-test</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <daemon>true</daemon>
                  <connectors>
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                      <port>${jetty.port}</port>
                    </connector>
                  </connectors>
                </configuration>
              </execution>
              <execution>
                <id>stop-jetty</id>
                <phase>post-integration-test</phase>
                <goals>
                  <goal>stop</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

I think with this TestNG test this should work … (the base class ImportantUseCaseTest contains some @Test annotated test methods):

public class IntegrationTest extends ImportantUseCaseTest {
  @Override
  protected String getServerURL() {
    final String jettyPort = System.getProperty("jetty.port", "8080);
    return String.format("http://localhost:%s/webapp", jettyPort);
  }
}

But it doesn’t work🙂

If I call maven with „-Djetty.port=1234“ the jetty starts on 1234 but the integration test runs on 8080.  Hmmm …

The solution was to change the surefire plugin configuration a little bit:

...
  <plugin>
     <artifactId>maven-surefire-plugin</artifactId>
     ...
     <configuration>
         ...
         <argLine>-Xmx512m -Djetty.port.integration=${jetty.port}</argLine>
     </configuration>

and the test class:

public class IntegrationTest extends ImportantUseCaseTest {
  @Override
  protected String getServerURL() {
    final String jettyPort = System.getProperty("jetty.port.integration", "8080);
    return String.format("http://localhost:%s/webapp", jettyPort);
  }
}

I guess that surefire with forkMode=once doesn’t pass the given system properties to the test jvm. Maybe with forkMode=never this problem not exists … but you can try this by yourself😉