title: Spring maven - run specific tests (via annotations or maven profile)
body: <p>Using a Spring maven context, I would like to run specific tests based on a maven profile. I would like to have an easy way of tagging the test groups. If possible I would like to use the annotations.
  Which options are there, like maven command line parameters, maven profiles specification, etc.</p>
  <p>Say I have the following tests:</p>
  <p>Example:</p>
  <pre><code>// annotation(&quot;integration&quot;)
  public class GeopointFormatterTest {
      @Test
      public void testIntegration1() {  ...  }   
      @Test
      public void testIntegration2() {  ...  }
  </code></pre>
  <p>Annotations like @Profile (which is for creating beans) and @ActiveProfile (which is for selecting specific profiles for creating beans) cannot be used for selecting tests, of course. All tests just run for statements like:</p>
  <blockquote>
  <p>mvn clean install -Pdevelopment</p>
  <p>mvn clean install -Pdevelopment -Dspring.profiles.active=acceptance</p>
  <p>mvn clean install -Pdevelopment -Dspring.profiles.active=integration</p>
  </blockquote>
  <p>As suggested, I used also @IfProfileValue. This is a good way for selecting tests based on system property values. System property values can be overruled by a CustomProfileValueSource class, like in: @ProfileValueSourceConfiguration(CustomProfileValueSource.class)</p>
  <p><strong>EDIT and ALTERNATIVE</strong></p>
  <p>The GREAT answers below focus on JUnit's @Category mechanism. Thanks to all!</p>
  <p>A different approach is via these steps: [1] set a property within a maven profile and [2] use the property to skip tests via the  of the standard surefire test plugin.</p>
  <p>[1] Setting the properties via a profile:</p>
  <pre><code>&lt;profiles&gt;
      &lt;profile&gt;
          &lt;id&gt;integrationtests&lt;/id&gt;
          &lt;properties&gt;
              &lt;integration.skip&gt;false&lt;/integration.skip&gt;
              &lt;acceptance.skip&gt;true&lt;/acceptance.skip&gt;
          &lt;/properties&gt;
      &lt;/profile&gt;
      ... other profiles
  </code></pre>
  <p>[2] Using the properties in the surefire test plugin to skip tests.</p>
  <pre><code>&lt;build&gt;
     &lt;plugins&gt;
        &lt;plugin&gt;
           &lt;!-- Run the integration test--&gt;
           &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
           &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
           &lt;version&gt;${surefire.plugin.version}&lt;/version&gt;
           &lt;configuration&gt;
              &lt;skipTests&gt;${acceptance.skip}&lt;/skipTests&gt;
  </code></pre>
  <p>Start in maven: mvn clean install –Pintegrationtests</p>