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.
Hi i found the solution.
I decided to stimulate open session in view in my test.
so this will be use for application in testing.