Announcement Announcement Module
Collapse
No announcement yet.
Problem with JPA testing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with JPA testing

    I am new to Spring and I have a problem when I try to test my dao class.

    This is my abstract class:

    Code:
    public abstract class AbstractDAO<E extends AbstractDomainObject> implements GenericDAO<E> {
    	
    	@PersistenceUnit(unitName="myPersistenceUnit")
    	protected EntityManagerFactory emf;
    	
    	private String tableName;
    	
    	public Integer save (E entity) throws DaoException {
    		EntityManager em = getEmf().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            try {
                tx.begin();
                em.persist(entity);
                tx.commit();
                return entity.getId();
            } catch (Exception ex) {
                if (tx != null && tx.isActive()) {
                    tx.rollback();
                }
                throw new DaoException(ex);
            } finally {
                em.close();
            }
    		
    	}
    	
    	public abstract Integer update(E entity) throws DaoException ;
    		
    	public void setEmf(EntityManagerFactory emf) {
    		this.emf = emf;
    	}
    
    	public EntityManagerFactory getEmf() {
    		return emf;
    	}
    
    	public void setTableName(String tableName) {
    		this.tableName = tableName;
    	}
    
    	public String getTableName() {
    		return tableName;
    	}
    
    }
    This is the class, that I want to test.

    Code:
    public class JPAProgramDAO extends AbstractDAO<WashingProgram> implements WashingProgramDAO{
    
    	@Override
    	public Integer update(WashingProgram entity)throws DaoException {
    		 EntityManager em = emf.createEntityManager();
    	        EntityTransaction tx = em.getTransaction();
    	        try {
    	            tx.begin();
    	            WashingProgram oldEntity = em.find(WashingProgram.class, entity.getId());
    	            oldEntity.setDescription(entity.getDescription());
    	            oldEntity.setName(entity.getName());
    	            tx.commit();
    	            return entity.getId();
    	        } catch (Exception ex) {
    	            if (tx != null && tx.isActive()) {
    	                tx.rollback();
    	            }
    	            throw new DaoException(ex);
    	        } finally {
    	            em.close();
    	        }
    	}
    
    }
    This is my test class:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"/applicationContextForTests.xml"})
    
    public class JPAProgramDAOTest{
    	
    	@Autowired
    	private JPADaoFactory jpaDAOfactory;
    	
    	private JPAProgramDAO jpaProgramDao;
    	
    	public void setJPADaoFactory(JPADaoFactory jpaDAOfactory){
    		this.jpaDAOfactory = jpaDAOfactory;
    	}
    	
    	@Before
    	public void setUp() throws Exception{
    		jpaProgramDao = (JPAProgramDAO) jpaDAOfactory.obtainWashingProgrammDAO();
    	}
    
    	@Test
    	public void testSave() throws DaoException {
    		WashingProgram washingProgram = new WashingProgram();
    		washingProgram.setDescription("some description");
    		washingProgram.setName("some program");
    		jpaProgramDao.save(washingProgram);
    	}
    }
    This is my Spring configuration, applicationContextTest.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schem...-beans-3.0.xsd
               http://www.springframework.org/schema/context     
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">
               
    <context:annotation-config/>  
           
    <bean id="test" class="stu.home.domain.TestEntity">
    </bean>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    	<property name="driverClassName" value="org.postgresql.Driver"/>
    	<property name="url" value="jdbc:postgresql://localhost:5432/service_test"/>
    	<property name="username" value="user"/>
    	<property name="password" value="password"/>
    	
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    
        <property name="dataSource" ref="dataSource" />
        
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        
        <property name="annotatedClasses">
        <list>
            <value>stu.home.domain.WashingProgram</value>
            <value>stu.home.domain.Price</value>
            <value>stu.home.domain.Vehicle</value>
        </list>
        </property>
    
        <property name="hibernateProperties">
          <value>
            hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
            hibernate.hbm2ddl.auto=create
          </value>
          </property>
    
    </bean>
    
    
    <bean id="persistenceUnitManager"
    class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    	<property name="persistenceXmlLocations" value="META-INF/persistence.xml"/>
    	<property name="defaultDataSource" ref="dataSource"/>
    </bean>
    
    <bean id="emf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    	<property name="persistenceUnitName" value="myPersistenceUnit"/>
    </bean>
    
    <bean id="daoFactory" class="stu.home.dao.jpaImpl.JPADaoFactory">
    </bean>
    
    <bean id="price" class="stu.home.dao.jpaImpl.JPAPriceDAO">
    	<property name="tableName" value="price"></property>
    </bean>
    
    <bean id="vehicle" class="stu.home.dao.jpaImpl.JPAVehicleDAO">
    	<property name="tableName" value="vehicle"></property>
    </bean>
    
    <bean id="program" class="stu.home.dao.jpaImpl.JPAProgramDAO">
    	<property name="tableName" value="washing_program"></property>
    	
    </bean>
    
    </beans>
    When I run my JPAProgramDAOTest I get Exception:

    Code:
    stu.home.dao.DaoException: java.lang.UnsupportedOperationException: The user must supply a JDBC connection
    	at stu.home.dao.AbstractDAO.save(AbstractDAO.java:76)
    	at stu.home.tests.dao.jpaImpl.JPAProgramDAOTest.testSave(JPAProgramDAOTest.java:64)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: java.lang.UnsupportedOperationException: The user must supply a JDBC connection
    	at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:30)
    	at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
    	at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
    	at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
    	at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
    	at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
    	at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:38)
    	at stu.home.dao.AbstractDAO.save(AbstractDAO.java:68)
    	... 30 more

    Can anybody help me?
    Sorry for my English, I am not a native speaker...

  • #2
    the solution of the problem

    I have already resolved this problem. I forgot to make a reference in EntityManagerFactory bean on the dataSource in applicationContext:

    Code:
    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    	<property name="persistenceUnitName" value="myPersistenceUnit"/>
    	<property name="dataSource" ref="dataSource"/>
    </bean>

    Comment

    Working...
    X