简体   繁体   English

使用Junit测试Hibernate DAO

[英]Testing Hibernate DAO using Junit

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. 我在我的项目中使用了Spring和Hibernate的组合,并且想要测试诸如SaveDelete方法之类的DAO方法。

daoFoundation is a wrapper class created over hibernateSession. daoFoundation是一个通过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 这是我在SpringJunit4ClassRunner的JUnit Runs中编写的内容
I created the subject object in my SetupMethod. 我在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? 这是否足够,还是我需要在我的测试类中另外编写任何内容?

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 . 最简单的方法是导入Spring应用程序上下文,在要测试的DAO中自动装配,然后将测试方法或整个类标记为@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. 这将创建一个Hibernate会话,运行您的测试,然后自动回滚事务,这样您就不会通过测试影响数据库状态。

Have a look at how to run unit tests with Spring here . 看看如何在这里使用Spring运行单元测试。 You can get Spring to create your entire application context using the @ContextConfiguration annotation. 您可以使用@ContextConfiguration批注让Spring创建整个应用程序上下文。 So if you create your database using an XML file called database-servlet.xml then you would annotate 因此,如果使用名为database-servlet.xml的XML文件创建database-servlet.xml则需要进行注释

@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. 您可以使用注释@RunWith(SpringJUnit4ClassRunner.class)将Spring TestContext Framework的功能与单元测试结合使用。 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. 这允许您执行诸如声明应该抛出的预期异常,运行定时测试,重复测试运行X次以及一堆其他很酷的东西。

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. 让我知道这是否有效。

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. 测试dao层的最佳方法是使用spring jdbctemplate将数据写入数据库,测试get和delete方法。 Then in the @after delete the records you wrote. 然后在@after中删除你写的记录。 Then use hibernate to write to your database and use jdbctemplate to read them back. 然后使用hibernate写入数据库并使用jdbctemplate读取它们。 Then delete your test rows. 然后删除您的测试行。 Anything less and all you are really doing is testing hibernate's caching. 你正在做的就是测试hibernate的缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM