用于 Java JUnit 测试的嵌入式 PostgreSQL [英] Embedded PostgreSQL for Java JUnit tests

查看:26
本文介绍了用于 Java JUnit 测试的嵌入式 PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有嵌入式 PostgreSql 以便我们可以对 PostgreSql 驱动的应用程序进行单元测试?

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.

Embedded 并不一定意味着它必须嵌入到 JVM 进程中.它也不一定需要使用内存中持久性.它应该由依赖管理(Maven,Gradle)自动加载,以便单元测试可以在每台机器上运行,而无需安装和配置本地 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.

推荐答案

不,从进程内可加载的数据库即库的意义上说,不存在嵌入式 PostgreSQL.PostgreSQL 是面向过程的;每个后端都有一个线程,它会产生多个进程来完成工作.作为图书馆没有意义.

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.

H2 数据库 支持 PostgreSQL SQL 方言的有限子集以及 PgJDBC 驱动程序的使用.

The H2 database supports a limited subset of the PostgreSQL SQL dialect and the use of the PgJDBC driver.

可以做的是initdb 一个新的临时数据库,以 pg_ctl 在一个随机端口上,这样它就不会与其他实例冲突,运行你的测试,然后使用 pg_ctl 停止它,最后删除临时数据库.

What you can do 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.

强烈建议您在非默认端口上运行临时 postgres,这样您就不会冒与运行测试的机器上任何本地安装的 PostgreSQL 冲突的风险.

I strongly recommend that you run the temporary postgres on a non-default port so you don't risk colliding with any locally installed PostgreSQL on the machine running the tests.

(嵌入式 PostgreSQL 在 ecpg,本质上是一个 PostgreSQL client 嵌入在 C 源代码 中作为基于预处理器的 C 语言扩展.它仍然需要一个正在运行的服务器,而且使用起来有点麻烦,不是很推荐.它的存在主要是为了使从各种其他数据库移植更容易.)

(There is "embedded PostgreSQL in the sense of ecpg, essentially a PostgreSQL client embedded in C source code as 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.)

这篇关于用于 Java JUnit 测试的嵌入式 PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆