0

I am having some junit test cases are running on jenkins, but some of then are failed on jenkins, however I have them ran successfully in my local laptop.org.hibernate.SessionException: Session is closed! at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1688) at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347) at com.test.ui.struts.module.config.ReadAction.executeImpl(ReadAction.java:144) at com.test.ui.struts.module.config.CrudDataAction.execute(CrudDataAction.java:120)

Here is the code I have in ReadAction.java

 session = sf.getCurrentSession();
    try
    {
        if (!session.getTransaction().isActive())
            session.beginTransaction();
        Criteria c = session.createCriteria(metaBean.getEntityName());
        setProjection(metaBean, c);

        // fetch 1 more row than we can return to see if we have limited the result set
        c.setMaxResults(_maxRows + 1);
        c.add(getRestrictions(metaBean, reqBean));
        List result = c.list();//This is line 144

I do not see any reason that the session will be close here, any ideal?

Jack Zhang
  • 304
  • 1
  • 9
  • 21
  • 3
    You should not be connecting to a real database using hibernate when you exercise your code using unit tests. Instead such code should be exercised through integration tests. That said, your most probable cause is that there is a configuration difference between your unit test environment and the jenkins environment as it relates to hibernate. – Krish Srinivasan Feb 27 '14 at 21:26
  • I do not see any problem to have database setup on jenkins sever and running junit test cases against the database. – Jack Zhang Feb 27 '14 at 22:10
  • Furthermore, I think this is not a configuration issue, I am pretty sure about that, if it is a configuration issue, all of the junit use cases will be failed. – Jack Zhang Feb 27 '14 at 22:11
  • Is it possible that Jenkins is running your tests in parallel and somehow sharing the session between the threads? – David Levesque Feb 28 '14 at 00:07
  • Yeah, I have this feeling as well, but base on the log information, jenkins is running them asynchronously. – Jack Zhang Feb 28 '14 at 00:10
  • As @algorithmic mentioned you should not connect to real database from unit tests. You can write unit tests using Mocking library such as Mockito - to mock DB connections. – 18bytes Feb 28 '14 at 05:35
  • I disagree that, there is nothing wrong with connecting to a real database to run unit tests. – Jack Zhang Feb 28 '14 at 06:32

1 Answers1

0

The databases are not to be connected during the testing. A test should run light and should take at max 18 seconds to run. Mockito or PowerMockito should be used and it should be used for the same. Since jenkins does many parallel tasks, it is highly probable that the UT runs on your local but not on CI. Using PowerMockito may fix it