title: Get Spring Boot management port at runtime when management.port=0
body: <p>I'm looking for advice on how to get the port that was assigned to the embedded Tomcat that is serving the <em>actuator</em> endpoint when setting <code>management.port</code> property to <code>0</code> in integration tests.</p>
  <p>I'm using Spring Boot 1.3.2 with the following <code>application.yml</code> configuration:</p>
  <pre><code>server.port: 8080
  server.contextPath: /my-app-context-path
  
  management.port: 8081
  management.context-path: /manage
  
  ...
  </code></pre>
  <p>My integration tests are then annotated with <code>@WebIntegrationTest</code>, setting the ports shown above to <code>0</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>@WebIntegrationTest({ &quot;server.port=0&quot;, &quot;management.port=0&quot; })
  </code></pre>
  <p>And the following utility class should be used to get access to the application configuration when doing full integration tests:</p>
  <pre class="lang-java prettyprint-override"><code>@Component
  @Profile(&quot;testing&quot;)
  class TestserverInfo {
      
      @Value( '${server.contextPath:}' )
      private String contextPath;
  
      @Autowired
      private EmbeddedWebApplicationContext server;
      
      @Autowired
      private ManagementServerProperties managementServerProperties
      
      
      public String getBasePath() {
          final int serverPort = server.embeddedServletContainer.port
          
          return &quot;http://localhost:${serverPort}${contextPath}&quot;
      }
      
      public String getManagementPath() {
          // The following wont work here:
          // server.embeddedServletContainer.port -&gt; regular server port
          // management.port -&gt; is zero just as server.port as i want random ports
  
          final int managementPort = // how can i get this one ?
          final String managementPath = managementServerProperties.getContextPath()
          
          return &quot;http://localhost:${managementPort}${managementPath}&quot;
      }
  }
  </code></pre>
  <p>I already know the standard port can be get by using the <code>local.server.port</code> and there seems to be some equivalent for the management endpoint named <code>local.management.port</code>. But that one seems to have a different meaning. The <a href="http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-discover-the-http-port-at-runtime" rel="nofollow noreferrer">official documentation</a> doesn't mention a way to do this.</p>
  <p>Is there currently any undocumented way to get a hand on that management port?
  <br/></p>
  <hr />
  <h1>Solution Edit</h1>
  <p>As I am using the <a href="https://spockframework.org/" rel="nofollow noreferrer">Spock Framework</a> and <a href="https://mvnrepository.com/artifact/org.spockframework/spock-spring" rel="nofollow noreferrer">spock-spring</a> module for testing my Spring Boot application, I have to initialize the application like this:</p>
  <pre class="lang-java prettyprint-override"><code>@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MyApplication.class)
  </code></pre>
  <p>Somehow spock-spring or the test-initialization seems to affect the evaluation of the <code>@Value</code> Annotation so that <code>@Value(&quot;${local.management.port}&quot;)</code> resulted in</p>
  <blockquote>
  <p>java.lang.IllegalArgumentException: Could not resolve placeholder
  'local.management.port' in string value &quot;${local.management.port}&quot;</p>
  </blockquote>
  <p>With your solution I knew the property existed, so I simply use the Spring <code>Environment</code> directly to retrieve the property-value at test runtime:</p>
  <pre class="lang-java prettyprint-override"><code>@Autowired
  ManagementServerProperties managementServerProperties
  
  @Autowired
  Environment environment
  
  public String getManagementPath() {
      final int managementPort = environment.getProperty('local.management.port', Integer.class)
      final String managementPath = managementServerProperties.getContextPath()
      
      return &quot;http://localhost:${managementPort}${managementPath}&quot;
  }
  </code></pre>