title: Spring integration tests with profile
body: <p>In our Spring web applications, we use the Spring bean profiles to differentiate three scenarios: development, integration, and production. We use them to connect to different databases or set other constants. </p>
  
  <p>Using Spring bean profiles works very well for the changing the web app environment.</p>
  
  <p>The problem we have is when our <em>integration test code</em> needs change for the environment. In these cases, the integration test loads the application context of the web app. This way we don't have to redefine database connections, constants, etc. (applying the DRY principle).</p>
  
  <p>We setup our integration tests like the following.</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = ["classpath:applicationContext.xml"])
  public class MyTestIT
  {
     @Autowired
     @Qualifier("myRemoteURL")  // a value from the web-app's applicationContext.xml
     private String remoteURL;
     ...
   }
  </code></pre>
  
  <p>I can make it run locally using <code>@ActiveProfiles</code>, but this is hard-coded and causes our tests to fail on the build server.</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = ["classpath:applicationContext.xml"])
  @ActiveProfiles("development")
  public class MyTestIT
  { ... }
  </code></pre>
  
  <p>I also tried using the <code>@WebAppConfiguration</code> hoping that it might somehow import the <code>spring.profiles.active</code> property from Maven, but that does not work.</p>
  
  <p>One other note, we also need to configure our code so that developers can run the web app and then run the tests using IntelliJ's test runner (or another IDE). This is much easier for debugging integration tests.</p>