-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to Unit Testing Hibernate?
PostPosted: Fri Mar 10, 2006 12:47 am 
Newbie

Joined: Fri Mar 10, 2006 12:20 am
Posts: 3
Can someone tell me how to Uniting Testing Hibernate?
Please give me an simple example.Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 6:38 am 
Newbie

Joined: Tue Oct 18, 2005 4:50 am
Posts: 3
Can you tell whether you want to unit test in JUnit or something other? And also tell exact problem that you are facing. B'coz unit testing with hibernate is not a substantially different thing at all.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 7:20 am 
Newbie

Joined: Fri Mar 10, 2006 12:20 am
Posts: 3
I want to test hbm.xml and the code that mapulate the DB.
There is a StrutsTestCase that can test struts. But is there a HibernateTestCase or something like that to test Hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 12:12 pm 
Newbie

Joined: Fri Mar 10, 2006 12:06 pm
Posts: 4
I've been writing basic unit tests for beans that call hibernate APIs to query the database using Junit as normal. The basic hibernate mapping types are of course just beans, so it's more interesting to test whether a given query returns objects populated as you'd expect than to test whether their bean getter/setters work.

The hard part is setting up a test fixture. I've been lazy and just assumed certain data is in the database on my test machine. A better way would be to use setUp() and tearDown() to pre-populate the database with test data.

Martin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 8:32 pm 
Newbie

Joined: Fri Mar 10, 2006 12:20 am
Posts: 3
Thanks!
But can you give me a simple example?I'm still confused about how to pre-populate data and remove them after test.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 9:32 pm 
Beginner
Beginner

Joined: Wed Nov 30, 2005 2:41 pm
Posts: 29
Here's our setup() - creates the db from scratch using schemaExport:

public void setUp() throws Exception {
super.setUp();
Properties properties = new Properties();
properties.load(new FileInputStream(getPropertiesFile()));
configuration = new Configuration();
configuration.addDirectory(getConfigurationDirectory());
configuration.setProperties(properties);
m_bInitDb = Boolean.valueOf(properties.getProperty(
"db.initialize", "true")).booleanValue();
if (m_bInitDb) {
final SchemaExport schemaExport = new SchemaExport(configuration,
properties);
schemaExport.setDelimiter(";");
schemaExport.create(true, true);
}
sessionFactory = configuration.buildSessionFactory();
}

Of course, this just creates empty tables; if you want data you'll need to add it!

We often use hypersonic sql db for junit testing, which supports an in-memory db, great for this purpose.


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