Spring Jpa Transaction Manager Junit Testing

Setup Jpa Transaction Manager

package com.sforce.jpa;

import javax.inject.Inject;
import javax.inject.Named;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.sforce.datasource.DataSourceConfigImpl;
import com.sforce.model.DWAccount;

@PropertySource("classpath:/salesforcesync.properties")
@Configuration
@EnableTransactionManagement
@Import( { DataSourceConfigImpl.class })
public class JpaConfigurationImpl {
	
	 @Inject private Environment environment;
	 @Inject @Named("testDwDataSource") private DataSource testDwDataSource;
		
	 @Bean
	 public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
	        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
	        localContainerEntityManagerFactoryBean.setDataSource(testDwDataSource);
	        localContainerEntityManagerFactoryBean.setPackagesToScan(DWAccount.class.getPackage().getName());
	       
	        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
	        jpaVendorAdapter.setGenerateDdl(false);
	        jpaVendorAdapter.setShowSql(true);
	        jpaVendorAdapter.setDatabasePlatform(environment.getProperty("dataSource.dialect"));
	        
	        localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
	        return localContainerEntityManagerFactoryBean;
	    }
	 
	 @Bean
	 public PlatformTransactionManager transactionManager() throws Exception {
		 JpaTransactionManager transactionManager = new JpaTransactionManager();
	     transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
	     return transactionManager;
	 }
}

Test Jpa Transaction Manager

package com.sforce.jpa;

import static org.junit.Assert.*;

import javax.inject.Inject;
import javax.inject.Named;


import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
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;

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
@ContextConfiguration(classes={JpaConfigurationImpl.class})
public class JpaTransactionConfigTest {

	@PersistenceContext
        EntityManager entityManager;
	
	@Test
	public void testDWAccountInsert() {
		DWAccount newDWAccount = new DWAccount();
		newDWAccount.setId(new Long(1234));
		entityManager.persist(newDWAccount);
                assertNotNull(entityManager.find(DWAccount.class, new Long(1234));
	}
}

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: