IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

Hibernate Junit problème


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    204
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 204
    Par défaut Hibernate Junit problème
    Bonjour,

    J'ai un souci avec Hibernate et Junit, si vous voulez je n'arrive pas à tester une méthode ça bloque ici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
    voilà mon code Junit
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public class UserDaoTests extends TestCase{
     
    	UserDao userDao = null;
    	@Override
    	protected void setUp() throws Exception {
    		userDao = new UserDaoImpl();
    	}
     
        public void testGetAllUsers(){
        	userDao.getUsers();
        }
     
    }
    ça lance un exception de type NullPointerException

    Mon fichier hibernate.cfg.xml est le 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
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.password">root</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/experiments</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <mapping class="appli.domain.User"/>
        </session-factory>
    </hibernate-configuration>
    Merci à votre aide

  2. #2
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 276
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 276
    Par défaut
    Essaie de trouver ce qui est à null, ou montre nous la stacktrace complète.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    204
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 204
    Par défaut
    ben ce qui est null c'est getHibernateTamplate()elle retourne null

    voilà le code :
    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
    public List<User> getUsers() {
    		try {
    			logger.info("getting all users in the database");
    			HibernateTemplate hibertmlp = getHibernateTemplate();
    			Session session = hibertmlp.getSessionFactory().getCurrentSession();
     
    			Query qry = session.createQuery("from User");
     
    			return qry.list();
     
    		} catch (DataAccessException e) {
    			// Critical errors : database unreachable, etc.
    			logger.error("Exception - DataAccessException occurs : "+e.getMessage()+" when getting the liste of all user on complete getUsers().");
    			return null;
    		}
    	}
    le stackTrac :
    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
    java.lang.NullPointerException
    	at springapp.dao.UserDaoImpl.getUsers(UserDaoImpl.java:52)
    	at springapp.dao.UserDaoTests.testGetAllUsers(UserDaoTests.java:14)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:616)
    	at junit.framework.TestCase.runTest(TestCase.java:164)
    	at junit.framework.TestCase.runBare(TestCase.java:130)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:120)
    	at junit.framework.TestSuite.runTest(TestSuite.java:230)
    	at junit.framework.TestSuite.run(TestSuite.java:225)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

  4. #4
    Membre Expert
    Avatar de Patriarch24
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2003
    Messages
    1 047
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2003
    Messages : 1 047
    Par défaut
    Il ne manquerait pas des déclarations de beans spring là-dedans par hasard ?

  5. #5
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 276
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 276
    Par défaut
    Patriarch a raison.
    Ou alors, tu n'as simplement pas chargé ton context spring.

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    204
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 204
    Par défaut
    Non je pense pas parce que on est pas encore arrivé à ce stade là, la déclaration des des paramètres de connexion à la base de données c'est dans le fichier hibernate.cfg.xml donc on as pas besoin de déclaraer une datasource pour ça le truc peut être dans le fichier de configuration d'Hibernate, il n'as pas été renseigné dans le code de test peut être qu'Hibernate n'arrive pas à le charger pour le moment je le mis dans /WEB-INF/classes


    Merci

  7. #7
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 276
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 276
    Par défaut
    getHibernateTemplate est une méthode du framework Spring.
    Il y a donc un souci avec ta conf Spring.

  8. #8
    Membre Expert
    Avatar de Patriarch24
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2003
    Messages
    1 047
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2003
    Messages : 1 047
    Par défaut
    donc on as pas besoin de déclaraer une datasource
    Oui mais il y a besoin d'une sessionFactory pour HibernateTemplate. Tu devrais lire les logs de Spring qui te diront sans doute qu'un bean n'est pas correctement initialisé.

Discussions similaires

  1. [Hibernate][Oracle] Problème de rapidité
    Par Saloucious dans le forum Hibernate
    Réponses: 7
    Dernier message: 27/11/2008, 11h00
  2. [Hibernate][Junit][Debutante] Problème de connection
    Par Lydiane dans le forum Hibernate
    Réponses: 2
    Dernier message: 01/10/2008, 15h56
  3. [Hibernate-MySQL] Problème de contraintes ?!
    Par n@n¤u dans le forum Hibernate
    Réponses: 9
    Dernier message: 03/08/2006, 16h19
  4. [Hibernate & Eclipse] problème mapping
    Par sonia_ppr dans le forum Hibernate
    Réponses: 4
    Dernier message: 04/05/2006, 14h32
  5. [Hibernate][Ibatis] Problème de performance..
    Par Saloucious dans le forum Hibernate
    Réponses: 2
    Dernier message: 29/10/2005, 13h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo