2014-09-23 42 views
0

我想用JUnit對我的DAO方法進行測試。我已經爲服務層定義了測試類,但此方法不適用於dao。在JUnit中爲DAO層找不到當前線程的會話

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:hibernate4Config.xml"}) 
public class UserServiceTest { 
    @Autowired 
    private SessionFactory sessionFactory; 
    @Autowired 
    private UserService userService; 

    private Session session; 


    @Before 
    public final void before() { 
     session = sessionFactory.openSession(); 
    } 

    @After 
    public final void after() { 
     session.close(); 
    } 
//tests 

當我試圖爲DAO運行測試中,我看到No Session found for current thread; nested exception is org.hibernate.HibernateException: No Session found for current thread

從DaoTest類我的代碼:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:hibernate4Config.xml"}) 
public class UserDaoTest { 
    @Autowired 
    private SessionFactory sessionFactory; 
    @Autowired 
    private IUserDao userDao; 

    private Session session; 


    @Before 
    public final void before() { 
     session = sessionFactory.openSession(); 
    } 

    @After 
    public final void after() { 
     session.close(); 
    } 
//tests... 

我發現不行,因爲我有annoted爲@Transactional服務類和@Service。但是我需要測試DAO層,對它有任何破解?

我hibernate4config.xml:

<?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" xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

    <context:property-placeholder location="classpath:persistence-mysql.properties" /> 
    <context:component-scan base-package="com.prokopenko.tfc" /> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="com.prokopenko.tfc.domain" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="${jdbc.driverClassName}" /> 
     <property name="url" value="${jdbc.url}" /> 
     <property name="username" value="${jdbc.user}" /> 
     <property name="password" value="${jdbc.pass}" /> 
    </bean> 

    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 


    <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

</beans> 

對於DAO我使用泛型,我的抽象超 -

public abstract class AbstractHibernateDao<T extends Serializable> implements IOperations<T> { 
    private Class<T> clazz; 

    @Autowired 
    private SessionFactory sessionFactory; 


    protected final void setClazz(final Class<T> clazzToSet) { 
     clazz = Preconditions.checkNotNull(clazzToSet); 
    } 

    @Override 
    public final T findOne(final long id) { 
     return (T) getCurrentSession().get(clazz, id); 
    } 

    @Override 
    public final List<T> findAll() { 
     return getCurrentSession().createQuery("from " + clazz.getName()).list(); 
    } 

    @Override 
    public final void create(final T entity) { 
     Preconditions.checkNotNull(entity); 
     // getCurrentSession().persist(entity); 
     getCurrentSession().saveOrUpdate(entity); 
    } 

    @Override 
    public final T update(final T entity) { 
     Preconditions.checkNotNull(entity); 
     return (T) getCurrentSession().merge(entity); 
    } 

    @Override 
    public final void delete(final T entity) { 
     Preconditions.checkNotNull(entity); 
     getCurrentSession().delete(entity); 
    } 

    @Override 
    public final void deleteById(final long entityId) { 
     final T entity = findOne(entityId); 
     Preconditions.checkState(entity != null); 
     delete(entity); 
    } 

    protected final Session getCurrentSession() { 
     return sessionFactory.getCurrentSession(); 
    } 

UserDAOImpl中,以便與@Repository

+0

我已經成功地使用了我的服務層,標註爲@ Transactional和'@ Service'。你可以發佈你的classpath:hibernate4Config.xml文件和DAO實現類嗎? – ndrone 2014-09-23 14:35:06

回答

1

好annoted爲您測試dao它需要註釋

@Transactional

對於那裏有一個到數據庫的活動會話。你可以將它包含在你的測試中,它應該使你的測試通過

+0

是的,thx,但@Transactional沒有讀只有真 – bearhunterUA 2014-09-23 15:43:28

+0

我現在看到有更新的方法來編輯帖子,以避免混淆 – Ernie 2014-09-23 15:45:09

+0

我在測試數據庫中使用創建下拉,我也測試插入和更新命令,只讀它將是不好的結果。 – bearhunterUA 2014-09-24 09:34:03

相關問題