In a previous post, I discussed the Hibernate mappings file and Ant tasks I'm using to create my Java entity classes and database tables. I've since created some classes that wrap the CRUD functions for these entities. I'll refer to these as my persistence classes. Of course these classes use the Hibernate framework (Transaction, Session, Query) and also have a little logic for dealing with the underlying relationship between the entities.
So now I need to test the code. Yes I know, ideally I'd have written the tests before writing my persistence classes. :) I'm using JUnit for unit testing and since both JUnit and Hibernate are quite popular I figured I could quickly locate some online examples of using the two together. Well, here's where things get a little complicated, or should I say controversial...
First off, I should probably refer to the testing of my persistence classes as integration testing and not unit testing. As this Stack Overflow post explains, a unit test "should be a stand-alone test which is not related to other resources" and the integration test is "similar to unit tests but with external resources (db, disk access)".
Putting aside the semantics, what's the best way to use JUnit to test your Hibernate related code? Here are some of the suggestions I've found:
- Don't do a straight test of Hibernate related code with JUnit, use Spring instead. Spring has a testing module for writing unit and integration tests.
- Mock the Hibernate layer by using a mocking framework like JMock. This enables you to focus your unit test on your code and removes any external frameworks (ie. Hibernate) from the test.
- Use JUnit extension DbUnit to put your database into a known state before test runs.
- Use an embedded, in-memory, database like HSQL for your testing. This removes the reliance on your external full-fledged RDMS.
For reference, here's a couple of forum posts where JUnit and Hibernate are discussed:
For now, I'm going against better judgement and testing my persistence code with straight JUnit tests against my MySQL database. I'll test the CRUD functions of my persistence classes and leave it at that.
I've created a JUnit test class for each of my persistence classes. Rather than creating a Hibernate SessionFactory with each test method, I'll create it once in the JUnit @BeforeClass method and close it in the @AfterClass method. Here's what it looks like along with one of the test methods:
@BeforeClassWhat do you think? Reader ideas and suggestions are encouraged and appreciated.
public static void oneTimeSetup() throws Exception {
logger.debug("Entering oneTimeSetup()");
// A SessionFactory is set up once for an application
sessionFactory = new Configuration()
.configure() // configures settings from hibernate.cfg.xml
.buildSessionFactory();
}
@AfterClass
public static void oneTimeTearDown() {
logger.debug("Entering oneTimeTearDown()");
if ( sessionFactory != null ) {
sessionFactory.close();
}
}
@Test
public void testCreateTag() {
logger.debug("Enter testCreateTag()");
// create new Tag object
Tag tag = new Tag("tag1");
TagPersister tagPersister = new TagPersister();
// Ask for a session using the JDBC information we've configured
Session session = sessionFactory.openSession();
assertTrue("Error while creating new Tag", tagPersister.createTag(tag, session));
}
I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.
ReplyDeleteBelieve me I did wrote an post about tutorials for beginners with reference of your blog.
Core Java interview questions and answers
Java training in Tambaram
Java training in Velachery
Java training in Omr
Oracle training in Chennai
Really awesome blog. Your blog is really useful for me. oracle training in chennai
ReplyDeleteThis is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me.
ReplyDeletedata science training in chennai
data science training in tambaram
android training in chennai
android training in tambaram
devops training in chennai
devops training in tambaram
artificial intelligence training in chennai
artificial intelligence training in tambaram
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletesap training in chennai
sap training in annanagar
azure training in chennai
azure training in annanagar
cyber security course in chennai
cyber security course in annanagar
ethical hacking course in chennai
ethical hacking course in annanagar
Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.
ReplyDeleteangular js training in chennai
angular js training in velachery
full stack training in chennai
full stack training in velachery
php training in chennai
php training in velachery
photoshop training in chennai
photoshop training in velachery
This is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me.
ReplyDeletehardware and networking training in chennai
hardware and networking training in omr
xamarin training in chennai
xamarin training in omr
ios training in chennai
ios training in omr
iot training in chennai
iot training in omr
Avast SecureLine VPN License Key 2022 software is unconscious of the sites the user accesses. Each application is freely used without any fear of recording. Avast VPN Free License Key
ReplyDeleteAdobe Animate CC Crack 22.0.8.217 Free Download Full 2022. How to Crack Adobe Animate 2022 22.0.8.217 ? · First download Adobe Animate Crack full .Adobe Animate Crack Reddit
ReplyDelete