-->
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.  [ 4 posts ] 
Author Message
 Post subject: Unit Testing when using Hibernate
PostPosted: Wed May 06, 2009 12:32 pm 
Beginner
Beginner

Joined: Wed May 06, 2009 12:23 pm
Posts: 23
Hi this is my first post here . I am a newbie with UnitTesting .

Please suggest me
I am using Unit Test to write Test Cases aganist Hibernate implementation and i am using HSQLDB
Code:
protected void setUp() throws Exception {
        Configuration configuration =
            new Configuration();
        configuration.setProperty(
            Environment.DRIVER,
            "org.hsqldb.jdbcDriver");
        configuration.setProperty(
            Environment.URL,
            "jdbc:hsqldb:mem:ProductDAOTest");
        configuration.setProperty(
            Environment.USER, "sa");
        configuration.setProperty(
            Environment.DIALECT,
            HSQLDialect.class.getName());
        configuration.setProperty(
            Environment.SHOW_SQL, "true");
}
Code:
public void testSave()
{
        SessionFactory sessionFactory =
            configuration.buildSessionFactory();

}

I am not using Spring Framework
Please tell me will it be a good approach if i get access to the SessionFactory and session inside my testMethod??


Top
 Profile  
 
 Post subject: Re: Unit Testing when using Hibernate
PostPosted: Wed May 06, 2009 1:00 pm 
Beginner
Beginner

Joined: Wed May 06, 2009 12:23 pm
Posts: 23
please share your valuable ideas and views ( I am not using Spring Framework)


Top
 Profile  
 
 Post subject: Re: Unit Testing when using Hibernate
PostPosted: Wed May 06, 2009 9:45 pm 
Beginner
Beginner

Joined: Wed May 06, 2009 12:23 pm
Posts: 23
anybody please.


Top
 Profile  
 
 Post subject: Re: Unit Testing when using Hibernate
PostPosted: Thu Apr 15, 2010 11:40 am 
Newbie

Joined: Thu May 31, 2007 3:27 am
Posts: 11
Your approach is almost good, but I prefer to initialize the session factory in the test "setup" class, or better, when the class is loaded (which means using the annotation @beforeClass in junit 4). This makes my test faster since hibernate is not constantly loading and parsing mapping files.

Code:
public abstract class AbstractHibernatePersistanceTest {
  protected static SessionFactory sessionFactory;
  public static String configFile="hibernate.cfg.xml";
 
  @BeforeClass
  public static void setupHibernate(){
    if (sessionFactory == null) {
      try {
        Configuration conf = createConfiguration(); /*implements this method to create your test configuration*/
        sessionFactory = conf.buildSessionFactory();
      } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new IllegalStateException(ex);
      }
    }
  }
 
  @AfterClass
  public static void closeSessionFactory(){
    if(sessionFactory!=null){
    sessionFactory.close();
    sessionFactory=null;
    }
  }
}



Then you can use the class atribute "sessionFactory" in your unit test.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.