title: Specifying a Maven profile when running JUnit tests within a Spring webapp
body: <p><strong>Background</strong></p>
  
  <p>/pom.xml</p>
  
  <pre class="lang-xml prettyprint-override"><code>...
  &lt;properties&gt;
      ...
      &lt;jdbc.driver&gt;com.mysql.jdbc.Driver&lt;/jdbc.driver&gt;
      &lt;jdbc.url&gt;jdbc:mysql://${database.host}/${database.name}&lt;/jdbc.url&gt;
      &lt;jdbc.user&gt;${database.user}&lt;/jdbc.user&gt;
      &lt;jdbc.password&gt;${database.password}&lt;/jdbc.password&gt;
      ...
  &lt;/properties&gt;
  ...
  &lt;profiles&gt;
      &lt;profile&gt;
          &lt;id&gt;dev&lt;/id&gt;
          &lt;properties&gt;
              ...
              &lt;database.name&gt;database&lt;/database.name&gt;
              &lt;database.host&gt;localhost&lt;/database.host&gt;
              &lt;database.user&gt;root&lt;/database.user&gt;
              &lt;database.password&gt;&lt;/database.password&gt;
              ...
          &lt;/properties&gt;
      &lt;/profile&gt;
  &lt;/profiles&gt;
  ...
  </code></pre>
  
  <p>/src/main/resources/database.properties</p>
  
  <pre><code>...
  jdbc.driver=${jdbc.driver}
  jdbc.url=${jdbc.url}
  jdbc.user=${jdbc.user}
  jdbc.password=${jdbc.password}
  ...
  </code></pre>
  
  <p>/src/main/resources/spring/applicationContext.xml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;beans ... xmlns:p="http://www.springframework.org/schema/p" ...&gt;
      ...
      &lt;bean
          id="dataSource"
          ...
          p:driverClassName="${jdbc.driver}"
          p:url="${jdbc.url}"
          p:username="${jdbc.user}"
          p:password="${jdbc.password}"
          ... /&gt;
      ...
  &lt;/beans&gt;
  </code></pre>
  
  <p>/src/<strong>test</strong>/java/com/company/project/service/MyItemServiceImplTest.java</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "/spring/applicationContext.xml" })
  public class MyItemServiceImplTest {
  
      @Resource
      private MyItemService myItemService;
  
      @Test
      public void testSave() {
          MyItem myItem = new MyItem();
          myItemService.save(myItem);
          ...
      }
  
  }
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>When running the tests, it throws an exception:</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
  ...
  Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [spring/applicationContext.xml]: Could not resolve placeholder 'database.password'
  ...
  </code></pre>
  
  <p>I guess it's because I need to run the tests while specifying the <code>dev</code> profile like I do when I launch the webapp (using <code>-P dev</code>). But I can't make it work. Is it even possible?</p>
  
  <p><strong>PS</strong></p>
  
  <p>The filtered applicationContext.xml file (i.e. the one in /target/classes/spring/applicationContext.xml) is identical to the one in /src/*, but the filtered database.properties file (i.e. /target/classes/database.properties) looks like</p>
  
  <pre><code>jdbc.driver=com.mysql.jdbc.Driver
  jdbc.url=jdbc:mysql://${database.host}/${database.name}
  jdbc.user=${database.user}
  jdbc.password=${database.password}
  </code></pre>
  
  <p>It means that from the pom.xml file to the .properties one, the properties have been well filtered, but within the pom.xml itself, the properties that depend on the chosen profile won't get filtered. Probably because I want to specify anywhere the profile I need when launching the tests. But as I said before, <code>-P dev</code> doesn't seem work with JUnit...</p>