title: SpringBoot junit tests with multiple TomcatServletContainerInitializer 's - BindException: Address already in use"
body: <p>I have spring boot app and currently all my test are running green if ai run them separately i.e. manually, but when i run the maven package command then all test run in a serial mode i.e. after each other. What happens  then is that i get:</p>
  
  <pre><code>"java.net.BindException: Address already in use".
  </code></pre>
  
  <p>Basically because the test are running in serial they all try to claim the socket 127.0.0.1:8081. And since the the socket is in a 
  TIME_WAIT state the second test is not able to claim that socket. i.e. the output of netstat -apn </p>
  
  <p>netstat -apn |grep -i 8080 </p>
  
  <pre><code>tcp   0      0 127.0.0.1:8080          127.0.0.1:33952         TIME_WAIT 
  </code></pre>
  
  <ul>
  <li><p>Now i have two different profiles configured i.e. Development and Production i.e. see below:</p>
  
  <blockquote>
    <p>@Configuration @Profile("Development") public class
    TomcatEmbededDevelopmentTesting1Profile extends
    SpringServletContainerInitializer { ... }</p>
    
    <p>@Configuration @Profile("Production") public class
    TomcatEmbededProductionProfile extends
    SpringServletContainerInitializer { .. }</p>
  </blockquote></li>
  </ul>
  
  <p>What I am looking after now is a feature to specify which customer TomcatServletContainerInitializer i can run i.e. i would have 5 different one all listening on different ports/sockets. 
  The problem is if I have 5 different TomcatEmbeded configuration classes that are all marked with "@Profile("Development")" then how do i tell junit to execute the one that i need. I have tired  using the @SpringApplicationConfiguration , but that does not fly.</p>
  
  <pre><code>@SpringApplicationConfiguration(classes = {
          ...
          TomcatEmbededDevelopmentTesting1Profile.class,
          ...
          } )
  </code></pre>
  
  <p>then i found an article on the net that explains that there is some magic annotation @IntegrationTest("server.port:0") which is suppose to randomize that port, but that did not worked for me as well. 
  What is the right way to do it in spring? Any hints are greatly appreciated.</p>
  
  <p>here is example of my test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @TestExecutionListeners(listeners={ ServletTestExecutionListener.class,
                                      DependencyInjectionTestExecutionListener.class,
                                      DirtiesContextTestExecutionListener.class,
                                      WithSecurityContextTestExecutionListener.class 
                                      }
          )
  
  @SpringApplicationConfiguration(classes = {
          SecurityWebApplicationInitializerDevelopment.class, 
          SecurityConfigDevelopment.class, 
          TomcatEmbededDevelopmentTesting1Profile.class,
          Internationalization.class, 
          MVCConfigDevelopment.class,
          PersistenceConfigDevelopment.class,
          ServerAuthenticationSuccessHandler.class,
          ServerRedirectAuthenticationSuccessHandler.class,
          LogicServiceRegistryPostProcessor.class
          } )
  @WebAppConfiguration
  @EnableWebSecurity
  @EnableWebSocket
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  //@IntegrationTest({"server.port=0", "management.port=0"})
  @ActiveProfiles("Development")
      public class quickTest extends TestCase {
  ..
  }
  </code></pre>