title: Eclipse Maven: run test with spring profile
body: <p>I want to run maven with different profile but it seems not working. 
  I created 2 differents java class for my JPAConfiguration :  </p>
  
  <p>JPAConfiguration.class and JPAConfigurationTest.class</p>
  
  <pre><code>@Configuration 
  @Profile({"dev"}) 
  @EnableTransactionManagement(proxyTargetClass = true) 
  @EnableJpaRepositories(basePackages = { "com.jle.athleges.model.repository", "com.jle.athleges.security.repository" }) 
  @ComponentScan(basePackages = { "com.jle.athleges.model.services", "com.jle.athleges.security.services" }) 
  public class JpaConfiguration { 
  
      @Bean 
      public DataSource dataSource() throws SQLException { 
          System.out.println("use dev"); 
          EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 
          builder.setName("dev"); 
          return builder.setType(EmbeddedDatabaseType.H2).build(); 
      } 
  
      @Bean 
      public EntityManagerFactory entityManagerFactory() throws SQLException { 
  
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
          vendorAdapter.setGenerateDdl(true); 
  
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 
          factory.setJpaVendorAdapter(vendorAdapter); 
          factory.setPackagesToScan("com.jle.athleges.model.entity", "com.jle.athleges.security.entity"); 
          factory.setDataSource(dataSource()); 
          factory.afterPropertiesSet(); 
          return factory.getObject(); 
  
      } 
  
      @Bean 
      public EntityManager entityManager(EntityManagerFactory entityManagerFactory) { 
          return entityManagerFactory.createEntityManager(); 
      } 
  
      @Bean 
      public PlatformTransactionManager transactionManager() throws SQLException { 
          JpaTransactionManager txManager = new JpaTransactionManager(); 
          txManager.setEntityManagerFactory(entityManagerFactory()); 
          return txManager; 
      } 
  
      @Bean 
      public HibernateExceptionTranslator hibernateExceptionTranslator() { 
          return new HibernateExceptionTranslator(); 
      } 
  
  } 
  
  @Configuration 
  @Profile({"test"}) 
  @EnableTransactionManagement(proxyTargetClass = true) 
  @EnableJpaRepositories(basePackages = { "com.jle.athleges.model.repository", "com.jle.athleges.security.repository" }) 
  @ComponentScan(basePackages = { "com.jle.athleges.model.services", "com.jle.athleges.security.services" }) 
  public class JpaConfigurationTest { 
  
      @Bean 
      public DataSource dataSource() throws SQLException { 
          System.out.println("use test"); 
          EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 
          builder.setName("test"); 
          return builder.setType(EmbeddedDatabaseType.H2).build(); 
      } 
  
      @Bean 
      public EntityManagerFactory entityManagerFactory() throws SQLException { 
  
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
          vendorAdapter.setGenerateDdl(true); 
  
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 
          factory.setJpaVendorAdapter(vendorAdapter); 
          factory.setPackagesToScan("com.jle.athleges.model.entity", "com.jle.athleges.security.entity"); 
          factory.setDataSource(dataSource()); 
          factory.afterPropertiesSet(); 
          return factory.getObject(); 
  
      } 
  
      @Bean 
      public EntityManager entityManager(EntityManagerFactory entityManagerFactory) { 
          return entityManagerFactory.createEntityManager(); 
      } 
  
      @Bean 
      public PlatformTransactionManager transactionManager() throws SQLException { 
          JpaTransactionManager txManager = new JpaTransactionManager(); 
          txManager.setEntityManagerFactory(entityManagerFactory()); 
          return txManager; 
      } 
  
      @Bean 
      public HibernateExceptionTranslator hibernateExceptionTranslator() { 
          return new HibernateExceptionTranslator(); 
      } 
  
  } 
  </code></pre>
  
  <p>In my pom.xml, I have this  : </p>
  
  <pre><code>&lt;project&gt; 
  
  … 
  
  &lt;dependencies&gt; 
  
  … 
  
  &lt;/dependencies&gt; 
  
      &lt;profiles&gt; 
          &lt;profile&gt; 
              &lt;id&gt;dev&lt;/id&gt; 
              &lt;activation&gt; 
                  &lt;activeByDefault&gt;true&lt;/activeByDefault&gt; 
              &lt;/activation&gt; 
          &lt;/profile&gt; 
          &lt;profile&gt; 
              &lt;id&gt;test&lt;/id&gt; 
          &lt;/profile&gt; 
      &lt;/profiles&gt; 
  
  
      &lt;build&gt; 
          &lt;finalName&gt;AthleGes&lt;/finalName&gt; 
          &lt;pluginManagement&gt; 
              &lt;plugins&gt; 
                  &lt;plugin&gt; 
                      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; 
                      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; 
                      &lt;version&gt;${maven-compiler-plugin.version}&lt;/version&gt; 
                      &lt;configuration&gt; 
                          &lt;source&gt;1.8&lt;/source&gt; 
                          &lt;target&gt;1.8&lt;/target&gt; 
                          &lt;debug&gt;true&lt;/debug&gt; 
                      &lt;/configuration&gt; 
                  &lt;/plugin&gt; 
  
              &lt;/plugins&gt; 
          &lt;/pluginManagement&gt; 
          &lt;plugins&gt; 
              &lt;plugin&gt; 
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; 
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt; 
                  &lt;version&gt;${maven-war-plugin.version}&lt;/version&gt; 
                  &lt;configuration&gt; 
                      &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt; 
                  &lt;/configuration&gt; 
              &lt;/plugin&gt; 
              &lt;plugin&gt; 
                  &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt; 
                  &lt;artifactId&gt;jetty-maven-plugin&lt;/artifactId&gt; 
                  &lt;version&gt;${jetty-maven-plugin.version}&lt;/version&gt; 
                  &lt;configuration&gt; 
                      &lt;jettyEnvXml&gt;src/test/resources/jetty-env.xml&lt;/jettyEnvXml&gt; 
                      &lt;scanIntervalSeconds&gt;10&lt;/scanIntervalSeconds&gt; 
                      &lt;stopKey&gt;foo&lt;/stopKey&gt; 
                      &lt;stopPort&gt;9999&lt;/stopPort&gt; 
                  &lt;/configuration&gt; 
                  &lt;executions&gt; 
                      &lt;execution&gt; 
                          &lt;id&gt;start-jetty&lt;/id&gt; 
                          &lt;phase&gt;pre-integration-test&lt;/phase&gt; 
                          &lt;goals&gt; 
                              &lt;goal&gt;run&lt;/goal&gt; 
                          &lt;/goals&gt; 
                          &lt;configuration&gt; 
                              &lt;scanIntervalSeconds&gt;0&lt;/scanIntervalSeconds&gt; 
                              &lt;daemon&gt;true&lt;/daemon&gt; 
                          &lt;/configuration&gt; 
                      &lt;/execution&gt; 
                      &lt;execution&gt; 
                          &lt;id&gt;stop-jetty&lt;/id&gt; 
                          &lt;phase&gt;post-integration-test&lt;/phase&gt; 
                          &lt;goals&gt; 
                              &lt;goal&gt;stop&lt;/goal&gt; 
                          &lt;/goals&gt; 
                      &lt;/execution&gt; 
                  &lt;/executions&gt; 
              &lt;/plugin&gt; 
          &lt;/plugins&gt; 
      &lt;/build&gt; 
  
  &lt;/project&gt; 
  </code></pre>
  
  <p>And finally, I have a test class :</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class) 
  @ActiveProfiles(profiles = {"test","dev"}) 
  @ContextConfiguration(classes = { JpaConfigurationTest.class, JpaConfiguration.class, SecurityConfig.class }) 
  @TestExecutionListeners({  
          DependencyInjectionTestExecutionListener.class, 
          DirtiesContextTestExecutionListener.class, 
          TransactionalTestExecutionListener.class, 
          }) 
  public class MemberServiceImpTest { 
  
      static Logger log = LoggerFactory.getLogger(MemberServiceImpTest.class); 
  
      @Autowired 
      private MemberService memberService; 
  
      @Autowired 
      private MemberRepository repository; 
  
      @Before 
      public void setUp(){ 
          repository.deleteAll(); 
      } 
  
      @Test 
      public void saveMember() { 
  
          log.debug("Start saveMember"); 
          Member a = new Member(); 
          a.setFirstname("aaa"); 
          a.setLastname("hhh"); 
          a.setId(0L); 
  
          Assert.assertNotNull(memberService.save(a)); 
  
          log.debug("End saveMember"); 
      } 
  
      @Test 
      public void getAllMembers() { 
          log.debug("Start getAllMember"); 
  
          long sizeBefore = repository.count(); 
          Member a = new Member(); 
          a.setFirstname("aaa"); 
          a.setLastname("hhh"); 
          a.setId(2L); 
  
          Member b = new Member(); 
          b.setFirstname("aaa"); 
          b.setLastname("hhh"); 
          b.setId(1L); 
          memberService.save(a); 
          memberService.save(b); 
  
          Assert.assertEquals(memberService.getAll().size(),sizeBefore + 2); 
          log.debug("End getAllMember"); 
      } 
  } 
  </code></pre>
  
  <p>When I run my unit test from Eclipse, it is working fine. If I move the profile in the test class from dev to test and from test to dev, it is working. I mean the test pass and the displayed message "use dev" or "use test" is displayed.  </p>
  
  <p>I want to run the tests from maven (with m2e) and I created this configuration :</p>
  
  <p><a href="https://i.stack.imgur.com/fYBQv.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fYBQv.png" alt="Eclipse-Maven-Configuration"></a></p>
  
  <p>But when I change the profile, test is always started.  </p>
  
  <p>I tried to activate profile from Maven -> Select Maven Profile but I have been the same result.  </p>
  
  <p>I miss something but I don't know what. I don't understand. </p>
  
  <p>Could you help me? </p>
  
  <p>Thanks  </p>