Bonjour,

J'ai un problème en faisant un test Junit dans une application Web avec Spring et Hibernate.

Mon erreur est la suivante :

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
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
	at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1125)
	at com.test.tests.TestUser.testSave(TestUser.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Je vous joins mon code.

Mon application-context.xml présent sous WEB-INF :

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
51
52
53
54
55
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
 
 
	<!-- post-processors for all standard config annotations -->
	<context:annotation-config />
	<context:component-scan base-package="com.test" />
 
 
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/test_manager" />
		<property name="username" value="root" />
		<property name="password" value="" />
	</bean>
 
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!--  <property name="jpaDialect">
			<bean class="${jpa.dialect}" />
		</property>-->
		<property name="packagesToScan" value="com.test.models" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="true" />
				<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
				<!-- false = On ne genere pas la BDD au demarrage -->
				<property name="generateDdl" value="true" />
			</bean>
		</property>
	</bean>
 
	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
 
	<!-- enable the configuration of transactional behavior based on annotations -->
	<tx:annotation-driven transaction-manager="txManager" />
 
</beans>

Mon web.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
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Test</display-name>
  <welcome-file-list>
    <welcome-file>accueil</welcome-file>
  </welcome-file-list>
 
 
 
  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/application-context.xml</param-value>
 
  </context-param>
 
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
 
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <include-prelude>/WEB-INF/views/taglibs.jsp</include-prelude>
    </jsp-property-group>
  </jsp-config>
  <servlet>
    <servlet-name>Accueil</servlet-name>
    <servlet-class>com.test.servlets.Accueil</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Inscription</servlet-name>
    <servlet-class>com.test.servlets.Inscription</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Accueil</servlet-name>
    <url-pattern>/accueil</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Inscription</servlet-name>
    <url-pattern>/inscription</url-pattern>
  </servlet-mapping>
 
 
</web-app>
Mon bean User :

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
51
52
53
54
55
56
57
58
59
60
61
/**
 * 
 */
package com.test.models;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
 
/**
 * @author
 *
 */
@Entity
@Table(name="T_User")
public class User {
 
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long Id;
 
	@Column(name="nom", length=50)
	private String Nom;
 
 
 
 
 
	public Long getId() {
		return Id;
	}
	public void setId(Long id) {
		Id = id;
	}
	public String getNom() {
		return Nom;
	}
	public void setNom(String nom) {
		Nom = nom;
	}
	/**
         * 
         */
	public User() {
		// TODO Auto-generated constructor stub
	}
 
	public User(Long id, String nom) {
		super();
		this.Id = id;
		this.Nom = nom;
	}
 
	public User(String name) {
		this.Nom = name;
	}
 
}
Sa DAO :

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
package com.test.dao.impl;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
import com.test.dao.UserDAO;
import com.test.models.User;
 
@Repository("UserDAO")
public class UserJPADAO extends GenericJPADAO<User, Long> implements UserDAO {
 
	@PersistenceContext
	private EntityManager entityManager;
 
 
	//public class UserJPADAO extends Dao<User> implements UserDAO {
	public UserJPADAO() {
		super(User.class);
	}
 
 
 
}
Mon service user :
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
51
52
53
54
55
56
57
 
package com.test.service.impl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.test.dao.UserDAO;
import com.test.dao.impl.UserJPADAO;
import com.test.models.User;
import com.test.service.IService;
 
 
 
 
 
@Service("userService")
 
public class UserService implements IService<User>{
 
	@Autowired
	private UserDAO userDAO;
 
 
 
 
	/**
         * Default Constructor
         */
	public UserService(){
 
	}
 
	private UserJPADAO userJPADAO;
 
	 public UserService(UserJPADAO userJPADAO) { 
	   this.userJPADAO=userJPADAO; 
	 }
 
	 @Transactional
	 public void save(User user){
 
		 userDAO.save(user);
		}
 
 
 
	@Override
	public User newInstance() {
		// TODO Auto-generated method stub
		return new User();
	}
 
 
}
Et pour finir ma classe test qui pose ce fameux problème :

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
51
package com.immo.tests;
 
 
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.web.context.support.XmlWebApplicationContext;
 
import com.immo.models.User;
import com.immo.service.impl.UserService;
 
public class TestUser {
 
    XmlWebApplicationContext   context = new XmlWebApplicationContext();
 
 
 
    @Before
    public void setUp() throws Exception {
 
 
    }
 
    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {
        context.close();
    }
 
 
 
    public TestUser() {
        // TODO Auto-generated constructor stub
    }
 
    @Test
    public void testSave() {
 
 
 
        UserService userService = context.getBean(UserService.class);
        User user=new User();
        user.setNom("Test");
        userService.save(user);
 
    }
 
}
J'espère qu'avec cela vous pourrez me donner un coup de main car je sèche vraiment sur cela (au point que je me suis peut-être un peu embrouillé en tout seul a force de chercher).



Merci par avance de vos réponse.