Bonjour,
Je débute un peu en spring et en Hibernate. J'ai hérité d'une application qui fonctionne avec Tomcat, Spring et Hibernate.
J'essaye de faire un Junit afin de tester une classe DAO.
Voici le fichier context.xml défini dans META-INF
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <Context path="/DBportailgestionprojet" docBase="DBportailgestionprojet" debug="5" reloadable="true" crossContext="true"> <Resource name="jdbc/portail" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="-1" username="userPortail" password="passPortail" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/portailgestionprojet?autoReconnect=true&useEncoding=true&characterEncoding=UTF-8" /> <Loader delegate="false" /> </Context>
Mes beans sont définis dans différents fichiers xml avec par exemple celui qui m'intéresse WEB-INF/config/commons-servlet.xml
Code xml : 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/portailgestionprojetDB" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="mappingResources"> <list> <value> com/portail/hibernate/Identification.hbm.xml </value> <value> com/portail/hibernate/Affectation.hbm.xml </value> .... <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="hibernateCurrentSession" class="com.portail.common.dao.impl.HibernateThreadImpl"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="projetDao" class="com.portail.common.dao.impl.ProjetDaoImpl"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> <property name="hibernateUtil"> <ref local="hibernateCurrentSession" /> </property> </bean> <!-- maps url to a controller --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/ConnectionUserController"> connectionUserController </prop> <prop key="/PageController"> pageController </prop> </props> </property> </bean> </beans>
Et voici ma classe de test
Quand je lance mon test j'ai l'erreur suivant
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 package com.portail.common.dao.impl; import static org.junit.Assert.fail; import java.util.Iterator; import java.util.List; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource; import com.portail.common.dao.interf.ProjetDao; import com.portail.persistence.Projet; public class ProjetDaoImplTest { private static XmlBeanFactory bf; private static ProjetDao projetDao; @BeforeClass public static void setUpBeforeClass() throws Exception { bf = new XmlBeanFactory(new FileSystemResource("E:/work/Portail/src/main/webapp/WEB-INF/config/commons-servlet.xml")); projetDao = (ProjetDao) bf.getBean("projetDao"); } @Test public void testListeProjetsCP() { List<Projet> listeProjets = projetDao.listeProjetsCP(16); Iterator<Projet> itProjet = listeProjets.iterator(); while (itProjet.hasNext()) { Projet projetCourant = itProjet.next(); System.out.println("Test " + projetCourant.getNom()); } } }
Merci pour votre aide.Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in file [E:\work\Portail\src\main\webapp\WEB-INF\config\commons-servlet.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
Partager