2014-09-23 75 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

相关问题