【问题标题】:arquillian using a random port with tomcat7 embededarquillian 使用嵌入 tomcat7 的随机端口
【发布时间】:2013-11-24 01:18:13
【问题描述】:

我想为 arquillian 使用随机端口。 所以在 arquillian.xml 我愿意:

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

在我的单元测试中:

@ArquillianResource
private URL base;

我希望拥有 Apache Tomcat 使用的真实端口 (localPort)(因为是的,它以随机端口开头),但此 URL 使用 0 端口,配置中的端口不是随机端口。

那么如何访问这个?

【问题讨论】:

    标签: jboss-arquillian embedded-tomcat-7


    【解决方案1】:

    您是否使用 Apache Maven 来运行此类测试? 这就是我的做法。在 Maven 方面,我使用 buildhelper 插件和 surefire 来定义随机端口并将其作为系统属性传递给测试

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

    然后我用 arquillian 配置了

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

    注意:当我从 IDE 启动测试时,我使用端口的默认值以避免手动配置它。

    HTH

    干杯,

    【讨论】:

    • 我知道!但是不,因为这在使用 IDE 时不起作用(不使用这个该死的 m2e)
    【解决方案2】:

    您可以使用 arquillian-available-port-extension。

    只需在你的 pom 中添加依赖项

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

    并放入你的 arquillian.xml :

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

    这具有在 Maven 中运行和在 IDE 中运行时都有效的优势。

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

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 2015-12-05
      相关资源
      最近更新 更多