-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 
Author Message
 Post subject: JUnit Test for HIbernate => Exception SessionFactory in J
PostPosted: Tue Dec 12, 2006 8:23 am 
Newbie

Joined: Tue Dec 12, 2006 8:17 am
Posts: 8
Hello, I need to test my DAO classes, but makes this error at
"dao = new UsuariosDAO();":
IllegalStateException: COuld not locate SessionFactory in JNDI.

What can I do? Thanks.


This is the code:

public class UsuariosDAOTest {

private static Usuarios usr = null;
private static UsuariosDAO dao = null;

@BeforeClass public static void runBeforeEachTest() {
usr = new Usuarios();
dao = new UsuariosDAO();
}

@Test public void getUsuarioByNamePass() throws Exception {
usr.setUsuario("USUARIO2");
usr.setPassword("USUARIO1");
usr = dao.getUsuarioByNamePass(usr);
assertNotNull("User found", usr.getUsuario());
}

@AfterClass public static void runAfterEachTest() {
usr = null;
dao = null;
}

public static junit.framework.Test suite() {
return new JUnit4TestAdapter(UsuariosDAOTest.class);
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 8:29 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
You test your DAO outside application server? If this is the case, there's no JNDI-context, where you can put your session factory, because without application server there's no naming server. You must create SessionFactory and put it to some other location.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 8:35 am 
Newbie

Joined: Tue Dec 12, 2006 8:17 am
Posts: 8
thanks.... and how can i makes a JNDI context? How can i create the SessionFactory?
Can I use HIbernateUtil methods?

public class HibernateUtil {

private static SessionFactory sf;
private static Configuration configuration;

static {
buildSessionFactory();
}

public static void buildSessionFactory() throws HibernateException {
if (sf == null) {
try {
configuration = new Configuration().configure();
sf = configuration.buildSessionFactory();
} catch (HibernateException e) {
}
}
}

public static SessionFactory getSessionFactory() throws HibernateException {
if (sf == null || sf.isClosed()) {
buildSessionFactory();
}
return sf;
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 8:40 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Yes, you can use HibernateUtil methods. In this case you put SessionFactory in static variable of that class.

You can't use JNDI when you don't have its implementation.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 9:09 am 
Newbie

Joined: Tue Dec 12, 2006 8:17 am
Posts: 8
Sorry, but I can't do this. I changed this method, and introduced the HibernateUtil.getSessionFactory(), but returns the same exception.
Please, can you tell me what can i do?

Thanks.

@BeforeClass public static void runBeforeEachTest() {
SessionFactory sf = HibernateUtil.getSessionFactory();
usr = new Usuarios();
dao = new UsuariosDAO();
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 9:16 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Did you remove session_factory_name setting in Hibernate.cfg.xml file for tests? If this property is omitted, the SessionFactory will not be bound to JNDI.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 9:24 am 
Newbie

Joined: Tue Dec 12, 2006 8:17 am
Posts: 8
Yes, I have removed it, but i think it isn't necessary because i am running JUnit Test from Eclipse.

The full trace error is:

java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
at bienestarsocial.bbdd.dao.UsuariosDAO.getSessionFactory(UsuariosDAO.java:40)
at bienestarsocial.bbdd.dao.UsuariosDAO.<init>(UsuariosDAO.java:30)
at bienestarsocial.junit.UsuariosDAOTest.runBeforeEachTest(UsuariosDAOTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.junit.internal.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
at org.junit.internal.runners.BeforeAndAfterRunner.runBefores(BeforeAndAfterRunner.java:50)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:33)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
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)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 9:35 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Can i watch your UsuariosDAO?

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 9:38 am 
Newbie

Joined: Tue Dec 12, 2006 8:17 am
Posts: 8
Yes, here it is....

public class UsuariosDAO {

private final SessionFactory sessionFactory = getSessionFactory();

protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext().lookup("SessionFactory");
}
catch (Exception e) {
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}


public void persist(Usuarios transientInstance) {
try {
sessionFactory.getCurrentSession().persist(transientInstance);
} catch (RuntimeException re) {
throw re;
}
}

public void attachDirty(Usuarios instance) {
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
} catch (RuntimeException re) {
throw re;
}
}

public void attachClean(Usuarios instance) {
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
} catch (RuntimeException re) {
throw re;
}
}

public void delete(Usuarios persistentInstance) {
try {
sessionFactory.getCurrentSession().delete(persistentInstance);
} catch (RuntimeException re) {
throw re;
}
}


public Usuarios getUsuarioByNamePass(String usuario, String password) {

try {

Lopd lopd = new Lopd(null);
String pwdencriptada = lopd.encripta(password);
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Usuarios.class);
criteria.add(Restrictions.eq("usuario", usuario));
criteria.add(Restrictions.eq("password", pwdencriptada));
Usuarios user = (Usuarios)criteria.uniqueResult();
return user;
} catch (RuntimeException re) {
throw re;
}
}


public Usuarios getUsuarioByNamePass(Usuarios usr) {
String usuario = usr.getUsuario();
String password = usr.getPassword();

return this.getUsuarioByNamePass(usuario, password);

}

public List getUsuarios() {
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Usuarios.class);

return criteria.list();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 9:49 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
But in this DAO you, as before, trying to find SessionFactory in JNDI. And when you can't find it, DAO throws IllegalStateException with exactly the same message you've found in console when you run the test. Again, when you run the test, you don't have JNDI-context!

You have two ways, one is to use ServiceLocator wich will find SessionFactory for your environment based on some kind of configuration (maybe properties file), another way is to use some framework with Dependency Injection capabilities.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 9:55 am 
Newbie

Joined: Tue Dec 12, 2006 8:17 am
Posts: 8
Yes... sorry but i don't know about this...
could you tell me how can i have JNDI-context?

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 10:23 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
You can't have it without app server! You, certainly, can access JNDI-context if you specify initial context factory class and provider url (with vendor specific values).

For example, for IBM WebSphere Application Server:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL, "corbaloc:iiop:myhost.mycompany.com:2809");
Context initialContext = new InitialContext(env);


But in this case you must have running app server, what, from my point of view, make no sense for unit tests. Consider one of the appoaches i point out before.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 3:47 am 
Newbie

Joined: Tue Dec 12, 2006 8:17 am
Posts: 8
I am running in JBoss and i proved with this, but it makes the same error in "dao = new UsuariosDAO();" line.... "COuld not locate session in JNDI."

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Context initialContext = new InitialContext(env);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 7:58 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Did you have application server running when you run the test? Did you provide session_name property in Hibernate.cfg.xml file?

Also, try to put one more environment setting:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
Context initialContext = new InitialContext(env);

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 6:04 am 
Newbie

Joined: Tue Dec 12, 2006 8:17 am
Posts: 8
Thank you very much for your help, but i cant do this...

I proved with the server running when I run the test, with and without session_name property in Hibernate.cfg.xml file, but it doesn't run....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.