Java 使用 Junit 测试 Hibernate DAO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23464255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing Hibernate DAO using Junit
提问by user3553237
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 的组合,并想测试 DAO 方法Save
和Delete
方法。
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
I created the subject object in my SetupMethod.
这是我在 JUnit Runs with SpringJunit4ClassRunner 中写的内容,我在 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?
这是足够的还是我需要在我的测试课中写任何额外的东西?
回答by mortsahl
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.
测试你的 dao 层的最好方法是使用 spring jdbctemplate 将数据写入你的数据库,测试你的 get 和 delete 方法。然后在@after 中删除你写的记录。然后使用 hibernate 写入您的数据库并使用 jdbctemplate 将它们读回。然后删除您的测试行。任何更少的事情,您真正要做的就是测试 hibernate 的缓存。
回答by JamesENL
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.
最简单的方法是导入您的 Spring 应用程序上下文,在您要测试的 DAO 中自动装配,然后将您的测试方法或整个类标记为@Transactional
. 这将创建一个 Hibernate 会话,运行您的测试,然后自动回滚事务,这样您的测试就不会影响您的数据库状态。
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
在此处查看如何使用 Spring 运行单元测试。您可以让 Spring 使用@ContextConfiguration
注释创建整个应用程序上下文。因此,如果您使用名为的 XML 文件创建数据库,database-servlet.xml
那么您将注释
@ContextConfiguration(locations={"classpath:/database-servlet.xml"})
public class Test()
@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.
您可以使用注释@RunWith(SpringJUnit4ClassRunner.class)
将 Spring TestContext Framework 的功能用于单元测试。这允许你做一些事情,比如声明应该抛出的预期异常,运行定时测试,重复测试运行 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.
让我知道这是否有效。