GWT Junit test causes Hibernate Exception

57 views
Skip to first unread message

VrmpX

unread,
Oct 7, 2010, 10:33:02 PM10/7/10
to Google Web Toolkit
Hello,

I'm currently using GWT 2.0.4 with Hibernate3, on Eclipse Helios, to
create a simple web application. So far, I'm able to reach the
database and do some RPC's to the server perfectly without any
problem.

The problem arises whenever I try to test out my server classes (the
data access objects) with a JUnit test case. I've added the JUnit.jar
(JUnit 3) and modified the "Run As..." properties to include the /src
and /test folders in the classpath. Whenever I call my class methods I
get the following exception:

Initial SessionFactory creation failed.java.lang.NoClassDefFoundError:
org/slf4j/impl/StaticLoggerBinder
at
com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:
62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
...
Caused by: java.lang.ExceptionInInitializerError
at com.ingsoftw.chat.util.HibernateUtil.<clinit>(HibernateUtil.java:
16)
at
com.ingsoftw.chat.server.UserDAOImpl.getUserByNick(UserDAOImpl.java:
15)
...
Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/
StaticLoggerBinder
at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:223)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:
111)
...
Caused by: java.lang.ClassNotFoundException:
org.slf4j.impl.StaticLoggerBinder
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)

The HibernateUtil.java code is:

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new
Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." +
ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

From what I understand, the problem lies on the fact that I haven't
added the sl4j.jar to the test server. Is the any way to do that?

Thanks in advance!

Lukasz

unread,
Oct 8, 2010, 1:41:05 PM10/8/10
to Google Web Toolkit
Can you post your actual test logic? I don't understand why do you
need to go through the GWT servlets to do some unit testing of your
DAO. It would be better to test the DAO logic separately.

VrmpX

unread,
Oct 9, 2010, 12:52:53 PM10/9/10
to Google Web Toolkit
Hi Lukasz,
Thanks for the reply. About my test logic (which I'm starting to
believe that it's wrong) is this:

I want to test my Hibernate DAO (let's call it UserDAO) which extends
GWT's RemoteService, so my project looks like this:

- GWT
- com.gwt.client
- interface userDAO
- interface userDAOAsync

- com.gwt.server
- class userDAOImpl

And the actual DAO code is

public class UserDAOImpl extends RemoteServiceServlet implements
UserDAO {

@Override
public User getUserByNick(String nick) {
Session session =
HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
User res =
(User)session.createCriteria(User.class).add(Restrictions.eq("nickname",
nick)).uniqueResult();
session.getTransaction().commit();
return res;
}
}

That's the reason I need to go through the GWT Servlets to test my
DAO, because they are the same thing (which is why I think I made a
mistake somewhere).

Do you think there is a better way to do the same thing? Like, maybe I
should make a DAO in its own class in the server package and call it
via a new RPC class.

Thanks for the time.

Lukasz

unread,
Oct 10, 2010, 1:10:51 PM10/10/10
to Google Web Toolkit
Usually you would separate this two layers of your application - you
would have a service layer and a DAO layer. The service classes
contains the logic accessible for the client and DAO takes care only
of the data access. In this case you can write nice unit tests for
your DAO and you don't need to run them in the GWT environment. When
testing the service layer, you can then mock the dao objects as they
already tested. For the DAO implementation I would recommend the
generic dao patter (http://www.ibm.com/developerworks/java/library/j-
genericdao.html)

HTH,
Lukasz

Jin

unread,
Oct 12, 2010, 3:48:17 AM10/12/10
to Google Web Toolkit
Hi,

Agree with Lukasz on separating the two layers.

Also, if your DAO class (or any other class you want to test) needs a
full J2EE container (which I suspect it might), then I don't think it
will run properly under JUnit. I ran into problems trying to test my
persistence code with JUnit because it needed a full J2EE container.

Cheers,

Jin
Reply all
Reply to author
Forward
0 new messages