Я хочу провести тесты для своих методов DAO с помощью JUnit. Я уже определил тестовый класс для уровня сервиса, но этот метод не работает для 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
Когда я пытаюсь запустить тесты для дао я вижу 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 annoted с @Repository
У меня был успех с моим сервисом, записанным как '@ Transactional' и' @ Service'. Можете ли вы опубликовать свой файл classpath: hibernate4Config.xml и класс реализации DAO? – ndrone