Testing Hibernate DAO using Junit

16,574

Solution 1

The easiest way is to import your Spring application context, autowire in the DAO's you want to test and then mark either your test methods or the entire class as @Transactional. This will create a Hibernate session, run your test and then automatically roll back the transaction so you don't effect your database state with your tests.

Have a look at how to run unit tests with Spring here. You can get Spring to create your entire application context using the @ContextConfiguration annotation. So if you create your database using an XML file called database-servlet.xml then you would annotate

@ContextConfiguration(locations={"classpath:/database-servlet.xml"}) public class Test()

You can use the annotation @RunWith(SpringJUnit4ClassRunner.class) to use functionality of the Spring TestContext Framework with your unit tests. This allows you to do things like declare expected exceptions that should be thrown, run timed tests, repeat test runs X times and a bunch of other cool stuff.

Basically to get this working, your test class should look similar to the following:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={YOUR APP CONTEXT FILES HERE AS A COMMA SEPARATED LIST})
public class Test(){
    @Autowired
    private YourDAO yourDAO;

    @Test
    @Transactional
    public void testSave(){
        //Test save method here. Any database changes made here will be
        //automatically rolled back when the test finishes.
    }

Let me know if that works or not.

Solution 2

The best way to test your dao layer is to use the spring jdbctemplate to write data to your database the test your get and delete methods. Then in the @after delete the records you wrote. Then use hibernate to write to your database and use jdbctemplate to read them back. Then delete your test rows. Anything less and all you are really doing is testing hibernate's caching.

Share:
16,574
user3553237
Author by

user3553237

Updated on June 13, 2022

Comments

  • user3553237
    user3553237 almost 2 years

    I am using a combination of Spring and Hibernate in my project and would like to test the DAO methods like Save and Delete methods.

    daoFoundation is a wrapper class created over hibernateSession.

    @Override
    public String createSubject(Subject subject) {
        String subjectId = (String) daoFoundation.save(subject);
        return subjectId;
    }
    

    This is what I wrote in my JUnit Runs with SpringJunit4ClassRunner
    I created the subject object in my SetupMethod.

    @Test
    public void createSubjectTest(){
        subjectDao.createSubject(subject);
        assertNotNull(hassSubjectSelection.getId());
    }
    

    Is this sufficient or do I need to write anything additional in my test class?

  • shadow
    shadow almost 6 years
    what if your files are not located in the classpath, can we still use this? @ContextConfiguration(locations={YOUR APP CONTEXT FILES HERE AS A COMMA SEPARATED LIST})