Bonjour,

Je réalise une application web ou j'utilise éclipse indigo, tomcat 7.0, Hibernate 4.1.1 et Spring 3.1.1.

Lorsque je veux tester une de mes classes avec JUnit, j'obtiens l'erreur :

org.hibernate.HibernateException: No Session found for current thread

J'en déduit que ma session n'est pas crée. Peut être que cela vient de la commande getCurrentSession().

voici mes fichiers :

application-context.xml

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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:aop="http://www.springframework.org/schema/aop"
	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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
 
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost/hibernate"/>
		<property name="username" value="root"/>
		<property name="password" value="admin"/>
	</bean>
 
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="annotatedClasses">
			<list>
				<value>App.Modele.FieldOwnedRecords</value>
				<value>App.Modele.OwnRecord</value>
				<value>App.Modele.Updatehistory</value>
				<value>App.Modele.Evaluation</value>
				<value>App.Modele.Soundtrack</value>
				<value>App.Modele.Playlist</value>
				<value>App.Modele.Comment</value>
				<value>App.Modele.Field</value>
				<value>App.Modele.Artist</value>
				<value>App.Modele.AuthenticatedUser</value>
        		<value>App.Modele.Record</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>	
			</props>
		</property>
	</bean>
 
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
 
	<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
	<context:annotation-config/>
	<context:component-scan base-package="App"></context:component-scan>
</beans>
classe à tester:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package App.Service;
 
 
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import App.Modele.AuthenticatedUser;
 
@Service("UserService")
@Transactional
public class UserServiceImpl implements UserService {
 
	@Autowired
	private SessionFactory sessionFactory;
 
 
	public boolean login(String username, String password) {
		Object[] params = {username, password};
		return sessionFactory.getCurrentSession().createFilter(params,"From authenticated_user Where username=? and password=?") != null;
	}
 
	@Override
	public void saveUser(AuthenticatedUser user) {
		sessionFactory.getCurrentSession().save(user);
	}
 
	@Override
	public void updateUser(AuthenticatedUser user) {
		sessionFactory.getCurrentSession().update(user);
	}
 
	@Override
	public AuthenticatedUser userProfile(String username) {
	return (AuthenticatedUser) sessionFactory.getCurrentSession().load(AuthenticatedUser.class,username);
 
	}
 
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
 
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
 
 
}
classe de test :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import static org.junit.Assert.*;
 
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import App.Service.UserService;
 
 
public class ServiceTest {
 
	private static ClassPathXmlApplicationContext context;
	private static UserService userService;
 
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		context=new ClassPathXmlApplicationContext("application-context.xml");
		userService = (UserService) context.getBean("UserService");
	}
 
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		context.close();
	}
 
	@Test
	public void testLogin() {
		userService.login("root", "admin");
	}
 
	@Test
	public void testSaveUser() {
		fail("Not yet implemented");
	}
 
	@Test
	public void testUpdateUser() {
		fail("Not yet implemented");
	}
 
	@Test
	public void testUserProfile() {
		fail("Not yet implemented");
	}
 
}
Merci d'avance