用于 Java JUnit 测试的嵌入式 PostgreSQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14314026/
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
Embedded PostgreSQL for Java JUnit tests
提问by blue123
Is there an embedded PostgreSql so that we could unit test our PostgreSql driven application?
是否有嵌入式 PostgreSql 以便我们可以对 PostgreSql 驱动的应用程序进行单元测试?
Since PostgreSql has some dialects, it's better to use embedded PostgreSql itself than other embedded databases.
由于 PostgreSql 有一些方言,因此最好使用嵌入式 PostgreSql 本身而不是其他嵌入式数据库。
Embedded does not necessarily mean it must be embedded in the JVM process. It also does not necessarily need to use in-memory persistence. It should be loaded automatically by the dependency management (Maven, Gradle), so that Unit tests can run on every machine without having to install and configure a local PostgreSQL server.
嵌入并不一定意味着它必须嵌入到 JVM 进程中。它也不一定需要使用内存中持久性。它应该由依赖管理(Maven,Gradle)自动加载,以便单元测试可以在每台机器上运行,而无需安装和配置本地 PostgreSQL 服务器。
采纳答案by Craig Ringer
No, there is no embedded PostgreSQL, in the sense of an in-process-loadable database-as-a-library. PostgreSQL is process oriented; each backend has one thread, and it spawns multiple processes to do work. It doesn' make sense as a library.
不,在进程内可加载的数据库即库的意义上,没有嵌入式 PostgreSQL。PostgreSQL 是面向过程的;每个后端都有一个线程,它会产生多个进程来完成工作。作为图书馆没有意义。
The H2 databasesupports a limited subset of the PostgreSQL SQL dialectand the use of the PgJDBC driver.
H2 数据库支持PostgreSQL SQL 方言的有限子集和 PgJDBC 驱动程序的使用。
What you cando is initdb
a new temporary database, start it with pg_ctl
on a randomized port so it doesn't conflict with other instances, run your tests, then use pg_ctl
to stop it and finally delete the temporary database.
你可以做的是initdb
一个新的临时数据库,pg_ctl
在一个随机端口上启动它,这样它就不会与其他实例冲突,运行你的测试,然后用pg_ctl
它来停止它,最后删除临时数据库。
I strongly recommendthat you run the temporary postgres on a non-default portso you don't risk colliding with any locally installed PostgreSQL on the machine running the tests.
我强烈建议您在非默认端口上运行临时 postgres ,这样您就不会冒与运行测试的机器上任何本地安装的 PostgreSQL 冲突的风险。
(There is"embedded PostgreSQL in the sense of ecpg, essentially a PostgreSQL clientembedded in C source codeas preprocessor based C language extensions. It still requires a running server and it's a bit nasty to use, not really recommended. It mostly exists to make porting from various other databases easier.)
(这里是在意义上的“嵌入式PostgreSQL的ECPG,基本上是一个PostgreSQL的客户端嵌入C源代码基于预处理C语言扩展,它仍然需要一个正在运行的服务器,这是一个有点坏坏地使用,不推荐使用。它主要存在于使从各种其他数据库移植更容易。)
回答by btiernay
The is an "embedded" PostgresSQL server that has been designed for unit testing from Java:
这是一个“嵌入式”PostgresSQL 服务器,专为从 Java 进行单元测试而设计:
https://github.com/yandex-qatools/postgresql-embedded
https://github.com/yandex-qatools/postgresql-embedded
Embedded postgresql will provide a platform neutral way for running postgres binary in unit tests. Much of the code has been crafted from Flapdoodle OSS's embed process
嵌入式 postgresql 将为在单元测试中运行 postgres 二进制文件提供一种平台中立的方式。大部分代码是通过Flapdoodle OSS 的嵌入过程制作的
As an aside, there also exists similar projects for Mongo, Redis, Memcachedand nodejs.
回答by Jan Zyka
I tried the project suggested by @btiernay (yandex-qatools). I spent a good few days with this and without any offence it's over engineered solution which doesn't work in my case as I wanted to download the binaries from internal repository rather than going to public internet. In theory it supports it but in fact it doesn't.
我尝试了@btiernay (yandex-qatools) 建议的项目。我花了好几天的时间,没有任何冒犯,它是过度设计的解决方案,在我的情况下不起作用,因为我想从内部存储库下载二进制文件而不是去公共互联网。理论上它支持它,但实际上它不支持。
OpenTable Embedded PostgreSQL Component
OpenTable 嵌入式 PostgreSQL 组件
I ended up using otj-pg-embeddedand it works like a charm. It was mentioned in comments so I thought I'll mention it here as well.
我最终使用了otj-pg-embedded,它就像一个魅力。它在评论中提到,所以我想我也会在这里提到它。
I used it as standalone DB and not via rule for both unit tests and local development.
我将它用作独立数据库,而不是通过单元测试和本地开发的规则。
Dependency:
依赖:
<dependency>
<groupId>com.opentable.components</groupId>
<artifactId>otj-pg-embedded</artifactId>
<version>0.7.1</version>
</dependency>
Code:
代码:
@Bean
public DataSource dataSource(PgBinaryResolver pgBinaryResolver) throws IOException {
EmbeddedPostgres pg = EmbeddedPostgres.builder()
.setPgBinaryResolver(pgBinaryResolver)
.start();
// It doesn't not matter which databse it will be after all. We just use the default.
return pg.getPostgresDatabase();
}
@Bean
public PgBinaryResolver nexusPgBinaryResolver() {
return (system, machineHardware) -> {
String url = getArtifactUrl(postgrePackage, system + SEPARATOR + machineHardware);
log.info("Will download embedded Postgre package from: {}", url);
return new URL(url).openConnection().getInputStream();
};
}
private static String getArtifactUrl(PostgrePackage postgrePackage, String classifier) {
// Your internal repo URL logic
}
回答by Arik
You can use a containerinstance of PostgreSQL.
Since spinning a container is a matter of seconds, this should be good enough for unittests.
Moreover, in case you need to persist the data, e.g. for investigation, you don't need to save the entire container, only the data files, which can be mapped outside of the container.
One of example of how to do this can be found here.
您可以使用PostgreSQL的容器实例。
由于旋转容器只需几秒钟,这对于单元测试来说应该足够了。此外,如果您需要保存数据,例如用于调查,您不需要保存整个容器,只需保存可以映射到容器外部的数据文件。
可以在此处找到如何执行此操作的示例之一。
回答by aramcodez
If you are looking to run an in-process version of postgres from an Integration (or similar) test suite, the postgresql-embeddedworked fine for me.
如果您希望从集成(或类似)测试套件运行 postgres 的进程内版本,则postgresql-embedded对我来说很好用。
I wrote a small maven pluginthat can be used as a maven wrapper around a forked version of postgresql-embedded.
我写了一个小的 maven 插件,它可以用作围绕 postgresql 嵌入的分叉版本的 maven 包装器。