简单可靠的内存数据库,用于支持 JPA 的快速 Java 集成测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3444265/
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
Simple and reliable in memory database for fast java integration tests with support for JPA
提问by topchef
My integration tests would run much faster if I used in-memory-database instead of PostgreSQL. I use JPA (Hibernate) and I need an in-memory-database that would be easy to switch to using JPA, easy to setup, and reliable. It needs to support JPA and Hibernate (or vice verse if you will) rather extensively since I have no desire to adopt my data access code for tests.
如果我使用内存数据库而不是 PostgreSQL,我的集成测试会运行得更快。我使用 JPA (Hibernate),我需要一个内存数据库,它可以轻松切换到使用 JPA、易于设置且可靠。它需要相当广泛地支持 JPA 和 Hibernate(或反之亦然),因为我不想采用我的数据访问代码进行测试。
What database is the best choice given requirements above?
鉴于上述要求,哪个数据库是最佳选择?
回答by Pascal Thivent
For integration testing, I now use H2(from the original author of HSQLDB) that I prefer over HSQLDB. It is faster(and I want my tests to be as fast as possible), it has some nice features like the compatibilitymode, the dev team is very responsive (while HSQLDB remained dormant for years until very recently).
对于集成测试,我现在使用H2(来自 HSQLDB 的原作者),我更喜欢 HSQLDB。它更快(我希望我的测试尽可能快),它有一些不错的功能,比如兼容模式,开发团队非常敏感(而 HSQLDB 多年来一直处于休眠状态直到最近)。
回答by brabster
I've been using HSQLDBin-memoryfor integration testing JPA/Hibernate persistence in Java. Starts pretty quickly, doesn't require any special setup.
我一直在使用内存中的HSQLDB来集成测试 Java 中的 JPA/Hibernate 持久性。启动非常快,不需要任何特殊设置。
The only issue I've seen so far with using HSQLDB with Hibernate was to do with batch size needing to be set to 0, but that might just have been related to an old version. I'll have a dig and see if I can find details of that problem.
到目前为止,我在 Hibernate 中使用 HSQLDB 时看到的唯一问题是批处理大小需要设置为 0,但这可能只是与旧版本有关。我会挖掘一下,看看我是否可以找到该问题的详细信息。
Derby supports an in-memory modethese days, it is no longer marked experimental.
现在,Derby 支持内存中模式,它不再被标记为实验性的。
回答by Archimedes Trajano
I use Derby. For one thing it is about 3 less lines of code per unit test since there is no need for a shutdown after the test. However, you need to use a JPA implementation that can drop and create tables such as EclipseLink.
我用德比。一方面,每个单元测试大约减少 3 行代码,因为测试后不需要关闭。但是,您需要使用可以删除和创建表的 JPA 实现,例如 EclipseLink。
Derby can also initialize a new in-memory database from a file so you can have a reference database and revert to it at anytime.
Derby 还可以从文件初始化一个新的内存数据库,这样您就可以拥有一个参考数据库并随时恢复到它。
For unit testing though, I prefer to create my objects in my unit test's @Before logic I find it easier especially with JPA as it allows me the flexibility to do refactorings and not have to worry about the underlying database structure, other tools such as DBunit rely on practically a static structure and refactoring implies changing of the DBunit XMLs manually rather than relying on Eclipse's refactoring capabilities.
不过对于单元测试,我更喜欢在我的单元测试的 @Before 逻辑中创建我的对象,我发现它更容易,尤其是使用 JPA,因为它允许我灵活地进行重构,而不必担心底层数据库结构,其他工具,如 DBunit实际上依赖于静态结构,重构意味着手动更改 DBunit XML,而不是依赖 Eclipse 的重构功能。

