Reputation: 3929
Hi I have the following problem when running junit test.
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:881)
at com.user.dao.UserDaoHibernateImpl.getUserByName(UserDaoHibernateImpl.java:40)
at com.user.service.UserServiceImpl.getUserName(UserServiceImpl.java:67)
My version:
hibernate 4.0.0 CR7 spring 3.1 CR2 wicket 1.5.3
@TransactionConfiguration(transactionManager = "test.transactionManager")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-test.xml"})
public class AccountPageTestCase extends AbstractTransactionalJUnit4SpringContextTests
{
private WicketTester tester;
@Test
public void signInUserMustGoToAccountPage() {
this.tester.startPage(AccountPage.class, new PageParameters());
final FormTester formTester = this.tester.newFormTester(AuthenticatedTestApplication.SIGN_IN_FORM_PATH);
formTester.setValue("username", AccountPageTestCase.TEST_USER);
formTester.setValue("password", AccountPageTestCase.TEST_USER);
formTester.setValue("rememberMeRow:rememberMe", false);
formTester.submit();
this.tester.assertRenderedPage(AccountPage.class);
this.tester.clickLink("accountBody:" + AuthenticatedTestApplication.SIGN_OUT_PANEL_PATH);
}
}
I added tx:annotation-driven in here.
<tx:annotation-driven transaction-manager="test.transactionManager" />
<bean id="test.transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
autowire="byType" />
So my getUserByName code
@Override
@Transactional(propagation = Propagation.SUPPORTS,
readOnly = true)
public User getUserByName(final String displayName) {
return this.userDao.getUserByName(displayName);
}
My accountpage constructor.
@SpringBean(name = "userService")
private transient UserService userService;
public AccountPage(final PageParameters pageParameters) {
final String userName = pageParameters.get("name").toString(StringUtils.EMPTY);
final User user = this.userService.getUserByName(userName);
}
the injection works but the transaction settings doesn't seem to work.
What is wrong? what would be the best way to solve the problem. I prefer not to use hibernate template and keep a non invasive hibernate approach.
My spring+hibernate test doesn't have the no session found for current thread but if i use wicket tester i get the problem.
Upvotes: 1
Views: 962
Reputation: 3929
Hi i found the solution.
I decided to stimulate open session in view in my test.
public void TestApp extends AuthenticatedWebApplication {
@Override
public void init() {
// open session in view for testing.
final SessionFactory sessionFactory = AuthenticatedTestApplication.context.getBean("sessionFactory", SessionFactory.class);
final Session s = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
}
@Override
protected void onDestroy() {
super.onDestroy();
final SessionFactory sessionFactory = AuthenticatedTestApplication.context.getBean("sessionFactory", SessionFactory.class);
final SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
}
so this will be use for application in testing.
Upvotes: 2
Reputation: 994
Maybe you should use mock daos to test you wicket page and write extra tests for you daos. It is much easier and much more useful to create more smaller tests instead of a few huge test cases.
https://cwiki.apache.org/WICKET/spring.html#Spring-UnitTestingtheProxyApproach
Upvotes: 0