Spring Custom Profiles Configuration and Testing

@ActiveProfiles for Junit

import static org.junit.Assert.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.example.config.HerokuDataSourceConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={JpaConfiguration.class, HerokuDataSourceConfiguration.class})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@ActiveProfiles(profiles={"heroku"})
@Transactional
public class CustomerJpaConfigTest {

	@PersistenceContext
	EntityManager entityManager;
	
	@Test
	public void testJpaConfiguration() {
		assertTrue(entityManager.isOpen());
	}

}

AnnotationConfigApplicationContext setActiveProfiles to active profile

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    applicationContext.getEnvironment().setActiveProfiles("heroku");
    applicationContext.scan(JdbcDoaImpl.class.getPackage().getName());
    applicationContext.refresh();

Maven run test for active profile

mvn jetty:run -Dspring.profiles.active="heroku"
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: