1

I'd like to use a random port for arquillian. So in arquillian.xml I do:

  <arquillian>
    <container qualifier="tomcat7" default="true">
      <configuration>
        ...
        <property name="bindHttpPort">0</property>
        ...
      </configuration>
    </container>
  </arquillian>

In my unit test:

@ArquillianResource
private URL base;

I hope to have the real port (localPort) used by Apache Tomcat (because yes it start with a random port) but this URL is with 0 port the one from configuration not random one.

So how to have access to this?

Olivier Lamy
  • 2,280
  • 1
  • 14
  • 12

2 Answers2

3

Are you using Apache Maven to run such tests ? Here is how I did. On Maven side I'm using the buildhelper plugin and surefire to define the random port and pass it to tests as a system property

<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
      <execution>
        <id>reserve-network-port</id>
        <phase>initialize</phase>
        <goals>
          <goal>reserve-network-port</goal>
        </goals>
        <configuration>
          <portNames>
            <portName>tomcat.http.port</portName>
          </portNames>
        </configuration>
      </execution>
    </executions>
  </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <systemProperties>
          <!-- Port used for Tomcat HTTP connector -->
          <tomcat.http.port>${tomcat.http.port}</tomcat.http.port>
        </systemProperties>
      </configuration>
    </plugin>
</plugins>

And then I configured arquillian with

<arquillian>
  <container qualifier="tomcat" default="true">
    <configuration>
      <property name="bindHttpPort">${tomcat.http.port:9090}</property>
    </configuration>
  </container
</arquillian>

Note : I'm using a default value for the port for when I'm launching the test from my IDE to avoid to have to manually configure it.

HTH

Cheers,

0

You can use the arquillian-available-port-extension.

Simply add the dependency in your pom

<dependency>
    <groupId>com.github.mryan43</groupId>
    <artifactId>arquillian-available-port-extension</artifactId>
    <version>${arquillian-available-port-extension.version}</version>
</dependency>

and put in your arquillian.xml :

<property name="bindHttpPort">${available.port}</property>

This has the advantage of working both when running in maven and when running in your IDE.

https://github.com/mryan43/arquillian-available-port-extension

mryan
  • 318
  • 3
  • 5