How to Mock a Hibernate Session with JMockit

I’m writing some unit tests with JUnit but my tests crash with NullPointerException in the line

Session session = sessionFactory.getCurrentSession();

There are also some session functions like flush and clear.

So I decided to write a mock object for that session, therefore tests can pass that line without got an exception.

Firstly, I wrote a mock for sessionFactoryImpl constructor ($init), because I need an object to call getCurrentSession, but creating a sessionFactory object is too complicated. So I wrote a mock for constructor and my mocked constructor working so simply because needs nothing, does nothing.

Then I mocked getCurrentSession, but the problem is what will it return in this new getCurrentSession. That function must return a new Session(), however SessionImpl’s constructor is private, it means that I cannot create a session object. Thus, I used a reflection to setAccessibleTrue for that constructor and then write a mock for that constructor, lastly create an object with that new constructor in getCurrentSession.

In summary, I made a mock for constructor in SessionImpl, then a mock for getCurrentSession and this mock return an SessionImpl object which created in my mocked constructor. Lastly another mock for constructor in sessionFactoryImpl to call getCurrentSession. So now I got a session that can do nothing :).

import org.hibernate.classic.Session;
import org.hibernate.SessionFactory;
import org.hibernate.impl.SessionFactoryImpl;
import org.hibernate.impl.SessionImpl;

private void HibernateSessionMock() {
       MockUp<SessionFactoryImpl> mockSessionFactory = new MockUp<SessionFactoryImpl>() {
           public void $init(Configuration cfg, Mapping mapping,
                   Settings settings, EventListeners listeners,
                   SessionFactoryObserver observer) {

           public Session getCurrentSession() throws IllegalArgumentException,
                   InstantiationException, IllegalAccessException,
                   InvocationTargetException, SecurityException,
                   NoSuchMethodException {
               Class<SessionImpl> clazz = SessionImpl.class;
               Constructor<SessionImpl> c = clazz.getDeclaredConstructor(
                       SessionImpl.class, EntityMode.class);
               c.setAccessible(true); // reflection
               Session session = c.newInstance(null, null);
               return session;

       SessionFactory sessionFactory = new SessionFactoryImpl(null, null,
               null, null, null);
       setField(instance, "sessionFactory", sessionFactory);
       //from import static mockit.Deencapsulation.setField;

       MockUp<SessionImpl> mockSessionImpl = new MockUp<SessionImpl>() {
           public void $init(SessionImpl parent, EntityMode entityMode) {


           public void flush() {


           public void clear() {


I hope it helps to you.

Tagged , ,

2 thoughts on “How to Mock a Hibernate Session with JMockit

  1. Rogerio says:

    A simplication is possible: in the mock implementation for “getCurrentSession()”, use Mockit.newEmptyProxy(Session.class) to create a “Session” instance where all methods do nothing.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: