title: spring boot tests - how to start embedded server before spring boot?
body: <p>When writing integration tests quite often there is a need to start some server (e.g. redis, zookeeper) on an ephemeral port and then pass the port as a property to Spring. The problem is that the actually used port is unknown before the server starts so it can't be passed via annotation:</p>
  
  <pre><code>@IntegrationTest(["redis.port=???"])
  class RedisTest {
      @Before
      before(){
         int port = startRedis(
      }
  }
  </code></pre>
  
  <p>What's the idiomatic way to achieve that? Ideally I'd like to put that into a junit rule.</p>
  
  <p>Here's something that works for me, but does not look good (passing port via static variable):</p>
  
  <pre><code>@IntegrationTest(initializers=Initializer.class)
  public class RedisTest {
      public static int port;
  
      public static class Initializer implements ApplicationContextInitializer&lt;ConfigurableApplicationContext&gt; {
          public void initialize(ConfigurableApplicationContext ctx){
              port = startRedis()
              EnvironmentTestUtils.addEnvironment(ctx, "redisPort=" + port)
          }
      }
  }
  </code></pre>